python结合匿名代理tor做高效爬虫

1、

    yum install tor -y
    yum install provity -y

3、

tor --hash-password mypassword

4、编辑:/etc/tor/torrc文件

ControlPort 9051
HashedControlPassword 16:872860B76453A77D60CA2BB0123042072093276A3D701AD684053EC4C

5、

/etc/init.d/tor restart

6、

      pip install stem

7、

forward-socks5 / localhost:9050 .

8、

/etc/init.d/privoxy restart
/etc/init.d/tor restart

9、示例代码:

from stem import Signal
from stem.control import Controller
import requests
def renew_connection():
    with Controller.from_port(port = 9051) as controller:
    controller.authenticate()
    controller.signal(Signal.NEWNYM)

for i in range(0, 10):
    renew_connection()
    print request.get(url="http://icanhazip.com/", proxy_ip={"http" : "127.0.0.1:8118"}).text

发表评论

电子邮件地址不会被公开。 必填项已用*标注