Python urllib2.urlopen()很慢,需要更好的方法来阅读几个网址

Jac*_*k z 12 python concurrency http urllib2

正如标题所示,我正在开发一个用python编写的网站,它会多次调用urllib2模块来读取网站.然后我用BeautifulSoup解析它们.

由于我必须阅读5-10个网站,因此页面需要一段时间才能加载.

我只是想知道是否有办法一次性阅读这些网站?或者是为了让它更快,我应该在每次阅读后关闭urllib2.urlopen,还是保持打开状态?

补充:另外,如果我只是切换到php,那么从其他网站获取和Parsi g HTML和XML文件会更快吗?我只是希望它加载更快,而不是目前需要的约20秒

Wai*_*ung 16

我正在使用像threading和的现代Python模块重写下面的Dumb Guy代码Queue.

import threading, urllib2
import Queue

urls_to_load = [
'http://stackoverflow.com/',
'http://slashdot.org/',
'http://www.archive.org/',
'http://www.yahoo.co.jp/',
]

def read_url(url, queue):
    data = urllib2.urlopen(url).read()
    print('Fetched %s from %s' % (len(data), url))
    queue.put(data)

def fetch_parallel():
    result = Queue.Queue()
    threads = [threading.Thread(target=read_url, args = (url,result)) for url in urls_to_load]
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    return result

def fetch_sequencial():
    result = Queue.Queue()
    for url in urls_to_load:
        read_url(url,result)
    return result
Run Code Online (Sandbox Code Playgroud)

最佳时间find_sequencial()是2s.最佳时间fetch_parallel()是0.9 秒.

thread由于GIL,在Python中说没用是不正确的.这是线程在Python中有用的情况之一,因为线程在I/O上被阻塞.正如您在我的结果中所看到的,并行情况要快2倍.

  • 是的,*如果*这是获取URL的唯一方法,这将更接近使用线程的正确方法.但是,async IO*仍然*会更快,更易于维护,允许确定性调试,等等.即使没有GIL,它也是一个出色的解决方案. (2认同)
  • Aaron,您能提供一个工作示例来证明异步IO代码更易于维护吗? (2认同)

Dum*_*Guy 9

编辑:请查看Wai的帖子以获取此代码的更好版本.请注意,此代码没有任何问题,它将正常工作,尽管下面的评论.

阅读网页的速度可能受到Internet连接的限制,而不是Python.

您可以使用线程一次加载它们.

import thread, time, urllib
websites = {}
def read_url(url):
  websites[url] = urllib.open(url).read()

for url in urls_to_load: thread.start_new_thread(read_url, (url,))
while websites.keys() != urls_to_load: time.sleep(0.1)

# Now websites will contain the contents of all the web pages in urls_to_load
Run Code Online (Sandbox Code Playgroud)

  • @Dumb Guy,不,它没有.GIL不是正确锁定的替代品,并且在所有python实现中都不存在.无论哪种方式,变异全局状态都是一种可怕的,*可怕的线程之间的通信方式.这就是`Queue`模块的用途. (2认同)