python3 urllib.request将永远阻止在gevent中

问题描述 投票:1回答:2

我想编写一个蜘蛛程序,在python 3中使用gevent下载网页。这是我的代码:

import gevent
import gevent.pool
import gevent.monkey
import urllib.request

gevent.monkey.patch_all()

def download(url):
    return urllib.request.urlopen(url).read(10)

urls = ['http://www.google.com'] * 100
jobs = [gevent.spawn(download, url) for url in urls]
gevent.joinall(jobs)

但是当我运行它时,会出现错误:

Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/gevent/greenlet.py", line 340, in run
result = self._run(*self.args, **self.kwargs)
File "e.py", line 8, in download
return urllib.request.urlopen(url).read(10)
File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
return opener.open(url, data, timeout)

......
return greenlet.switch(self)
gevent.hub.LoopExit: This operation would block forever
<Greenlet at 0x7f4b33d2fdf0: download('http://www.google.com')> failed with LoopExit
......

似乎urllib.request阻塞,所以程序无法工作。怎么解决?

python web-crawler block gevent
2个回答
0
投票

这可能是由于代理在公司网络内时的设置。个人推荐使用Selenium结合美丽的汤,使用浏览器打开网址链接,你可以下载HTML内容或直接控制浏览器。希望能帮助到你

from selenium import webdriver
from bs4 import BeautifulSoup
browser = webdriver.Ie()
url = "http://www.google.com"
browser.get(url)
html_source = browser.page_source
soup = BeautifulSoup(html_source, "lxml")
print(soup)
browser.close()

0
投票

Python, gevent, urllib2.urlopen.read(), download accelerator相同的问题。

从上述帖子重申:

read的参数是多个字节,而不是偏移量。

也:

您正在尝试读取来自不同greenlet的单个请求的响应。

如果您想使用多个并发连接下载相同的文件,那么您可以使用Range http标头(如果服务器支持它)(对于具有Range标头的请求,您将获得206状态而不是200)。请参阅HTTPRangeHandler。

© www.soinside.com 2019 - 2024. All rights reserved.