智能屏幕抓取使用不同代理和用户代理随机切换?
我想从http://abc.com/view_page.aspx?ID=下载一些HTML页面。这个ID是来自一个不同数字的数组。
我想访问这个网址的多个实例,并使用不同的代理IP和端口把文件保存为[ID].HTML。
我还想使用不同的用户代理,并且在每次下载之前随机设置等待时间。
有什么好的方法可以做到这一点呢?是用urllib2?pycURL?还是cURL?你们觉得哪个更适合这个任务?
请给点建议。谢谢大家!
3 个回答
2
如果你不想使用开放的代理,可以看看 ProxyMesh,这个服务会帮你自动更换和随机化IP地址。
2
可以使用Unix工具 wget
。它有选项可以设置自定义的用户代理和每次获取页面之间的延迟。
你可以查看 wget(1) 手册页 来获取更多信息。
5
可以使用类似这样的东西:
import urllib2
import time
import random
MAX_WAIT = 5
ids = ...
agents = ...
proxies = ...
for id in ids:
url = 'http://abc.com/view_page.aspx?ID=%d' % id
opener = urllib2.build_opener(urllib2.ProxyHandler({'http' : proxies[0]}))
html = opener.open(urllib2.Request(url, None, {'User-agent': agents[0]})).read()
open('%d.html' % id, 'w').write(html)
agents.append(agents.pop()) # cycle
proxies.append(proxies.pop())
time.sleep(MAX_WAIT*random.random())