请求保留我的IP地址,而我正在使用Tor

时间:2019-01-20 20:55:15

标签: python beautifulsoup python-requests http-proxy

当我执行python请求时,我注意到这是发送给请求的实际IP地址,而我使用Tor设置了一个新IP地址。 这是我的代码:

from torrequest import TorRequest

tr = TorRequest(proxy_port=9050, ctrl_port=9051, password=r"mypassword")
response = tr.get('http://ipecho.net/plain')
proxies = {'http': "socks5://"+response.text+":9050"}

page_response = requests.get('https://www.google.com/search?&q=Apple', timeout=60, verify=False, headers={'User-Agent': random.choice(user_agents)}, proxies=proxies)
soup = BeautifulSoup(page_response.content, 'html.parser')

但是,google意识到这仍然是我的IP地址,而不是Tor生成的IP地址。 怎么会来?

1 个答案:

答案 0 :(得分:0)

您如何知道Google在使用代理时可以获取您的实际IP?您使用的代理可能会被Google阻止,或者代理在首次连接时超时。

要了解这些可能的原因,您可以像这样->

from torrequest import TorRequest

tr = TorRequest(proxy_port=9050, ctrl_port=9051, password=r"mypassword")
response = tr.get('http://ipecho.net/plain')
proxies = {'http': "socks5://"+response.text+":9050"}

# Using this check, you will know weather your proxies are working or not.
# if proxy for request and current ip are same than proxy is working
try:
    print "The proxy for request is {0}".format(response.text)
    proxy_check = requests.get('http://icanhazip.com', timeout=60, proxies=proxies)
    print "Proxy is {0}".format(proxy_check)
except requests.exceptions.RequestException as e:
    print e

# we should catch request exception to check any exception raise from requests like 
# timeout
try:
    page_response = requests.get('https://www.google.com/search?&q=Apple', timeout=60, verify=False, headers={'User-Agent': random.choice(user_agents)}, proxies=proxies)
except requests.exceptions.RequestException as e:
    print e
soup = BeautifulSoup(page_response.content, 'html.parser')

现在,您可以知道要使用哪个IP来访问Google。如果代理是好的,那么谷歌很可能已经阻止了该代理。