我正在开发一个网络爬虫来生成一些网络流量,我想在访问所需的网站后我需要使用 tor (reset_identity())。我还需要我的连接才能通过德国特别出口。我一直在使用几个库(stem 和 torrequest),每个库都遇到了死胡同……
import torrequest
import requests
with torrequest.TorRequest(password=None) as tr:
response = requests.get('http://ipecho.net/plain')
print("My Original IP Address:", response.text)
tr.reset_identity() # Reset Tor
response = tr.get('http://ipecho.net/plain')
print("New Ip Address", response.text)
工作得很好,我得到了两个 ip 地址:第一个是我的 ip 地址,第二个是 tor 出口节点。但是我仍然需要它通过德国退出,所以我使用了launch_tor_with_config()。
torrequest.launch_tor_with_config(
tor_cmd='/usr/bin/tor',
config={
'ExitNodes': '{DE}' # exiting through Germany
}
)
但是从这里开始,我不知道如何处理获取请求。我试过了:
import requests
import stem.process
tor_process = stem.process.launch_tor_with_config(tor_cmd='/usr/bin/tor',
config={
'ExitNodes': '{DE}' # exiting through Germany
}
)
response = tor_process.communicate(requests.get('http://ipecho.net/plain'))
print("New Ip Address", response.text)
它以代码 1 结束:
Traceback (most recent call last):
File "/root/PycharmProjects/webtraffic/webtraffic1.py", line 10, in <module>
response = tor_process.communicate(requests.get('http://ipecho.net/plain'))
File "/usr/lib/python3.7/subprocess.py", line 964, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.7/subprocess.py", line 1667, in _communicate
self.stdin.flush()
ValueError: flush of closed file
Process finished with exit code 1
我在带有python3.7的Windows 10主机上使用guest kali linux。
提前感谢一堆!