【问题标题】:Requests keeping my IP address whereas I'm using Tor在我使用 Tor 时请求保留我的 IP 地址
【发布时间】:2019-06-14 07:22:17
【问题描述】:

当我执行 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')

但是,谷歌意识到它仍然是我的 IP 地址,而不是 Tor 生成的。 怎么会?

【问题讨论】:

    标签: python beautifulsoup python-requests http-proxy


    【解决方案1】:

    您如何知道 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。如果代理很好,那么谷歌很可能已经阻止了该代理。

    【讨论】:

    • 我已经粘贴了你的代码,但是当谷歌阻止我时,它会显示我的真实 IP 地址。所以还是一样的错误...
    • 这个检查是哪个IP返回proxy_check = requests.get('http://icanhazip.com', timeout=60, proxies=proxies)。如果这打印您的真实 IP,则代理无法正常工作。如果它打印您的代理 ip,那么 google 就会禁止您使用该代理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2015-12-14
    • 2022-12-20
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多