python 异步采集,网页爬虫编写 | 一步一步学python

python 异步采集,网页爬虫编写 | 一步一步学python,第1张

python 异步采集,网页爬虫编写 | 一步一步学python

python 异步采集

对于大量的数据采集除了多线程,就只有异步来实现了

上一篇文章里我写了有关多线程的实现,对于异步的由于本人对python的学习还不是很深入还不能自己写出来,刚好看到一个篇使用twisted异步采集的文章,就搬过来给大家分享一下。

Async Batching with Twisted: A Walkthrough

Example 1: Just a DefferedList

from twisted.internet import reactor
from twisted.web.client import getPage
from twisted.internet.defer import DeferredList
 
def listCallback(results):
  print results
 
def finish(ign):
  reactor.stop()
 
def test():
  d1 = getPage('http://www.google.com')
  d2 = getPage('http://yahoo.com')
  dl = DeferredList([d1, d2])
  dl.addCallback(listCallback)
  dl.addCallback(finish)
 
test()
reactor.run()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/2086473.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存