Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 python lxml 库来解析 html 页面:
import lxml.html # this might run indefinitely page = lxml.html.parse('http://stackoverflow.com/')
有没有办法设置解析超时?
它看起来被urllib.urlopen用作开启程序,但最简单的方法就是修改套接字处理程序的默认超时。
urllib.urlopen
import socket timeout = 10 socket.setdefaulttimeout(timeout)
当然,这是一个快速而肮脏的解决方案。