使用 python lxml 设置解析网页的超时

发布于 2024-08-31 16:45:13 字数 185 浏览 6 评论 0原文

我正在使用 python lxml 库来解析 html 页面:

import lxml.html

# this might run indefinitely
page = lxml.html.parse('http://stackoverflow.com/')

有没有办法设置解析超时?

I am using python lxml library to parse html pages:

import lxml.html

# this might run indefinitely
page = lxml.html.parse('http://stackoverflow.com/')

Is there any way to set timeout for parsing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪#恶を代价 2024-09-07 16:45:13

它看起来使用 urllib.urlopen 作为开启器,但最简单的方法只是修改套接字处理程序的默认超时。

import socket
timeout = 10
socket.setdefaulttimeout(timeout)

当然,这是一个快速而肮脏的解决方案。

It looks to be using urllib.urlopen as the opener, but the easiest way to do this would just to modify the default timeout for the socket handler.

import socket
timeout = 10
socket.setdefaulttimeout(timeout)

Of course this is a quick-and-dirty solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文