如何使用Tor与Pycurl?
你好,我想让我的爬虫程序使用Pycurl来连接Tor网络。我该怎么做呢?我知道如何用httplib来实现这个。
proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
print opener.open('http://www.google.com').read()
请帮帮我。
1 个回答
3
在Ubuntu上,我可以让pycurl通过tor使用socks5代理来工作:
import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://whatismyipaddress.com/")
c.setopt(pycurl.PROXY, "127.0.0.1")
c.setopt(pycurl.PROXYPORT, 9050)
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
c.perform()
别忘了安装tor
sudo apt-get install tor
要检查tor是否启动,可以运行网络监控命令:
sudo netstat -lnptu
运行后会得到类似这样的输出。请注意,tor是在127.0.0.1:9050这个地址上运行的
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:49017 0.0.0.0:* LISTEN 2701/skype
tcp 0 0 127.0.0.1:9050 0.0.0.0:* LISTEN 1810/tor
tcp 0 0 0.0.0.0:17500 0.0.0.0:* LISTEN 2187/dropbox