gpt4 book ai didi

python - 未找到 eventlet.green.urllib2 模块

转载 作者:行者123 更新时间:2023-12-01 02:28:02 24 4
gpt4 key购买 nike

我正在尝试在 http://eventlet.net/doc/examples.html 上运行第一个代码示例,webcrawler.py:

#!/usr/bin/env python
"""
This is a simple web "crawler" that fetches a bunch of urls using a pool to
control the number of outbound connections. It has as many simultaneously open
connections as coroutines in the pool.

The prints in the body of the fetch function are there to demonstrate that the
requests are truly made in parallel.
"""
import eventlet
from eventlet.green import urllib2


urls = [
"https://www.google.com/intl/en_ALL/images/logo.gif",
"http://python.org/images/python-logo.gif",
"http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif",
]


def fetch(url):
print("opening", url)
body = urllib2.urlopen(url).read()
print("done with", url)
return url, body


pool = eventlet.GreenPool(200)
for url, body in pool.imap(fetch, urls):
print("got body from", url, "of length", len(body))

但是,这会导致

ModuleNotFoundError: No module named 'urllib2'

我使用的是 eventlet 版本 0.21.0。该模块已移至 eventlet 中吗?

最佳答案

这些示例似乎已经过时(现在更是如此)。

对于 python 2,使用这个:

import eventlet
from eventlet.green import urllib2 as request

request.urlopen(...)

对于 python 3,使用这个:

import eventlet
from eventlet.green.urllib import request

request.urlopen(...)

关于python - 未找到 eventlet.green.urllib2 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47171621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com