【问题标题】:Requests + Proxy Servers, IP address won't change请求 + 代理服务器,IP 地址不会改变
【发布时间】:2017-12-08 00:42:39
【问题描述】:

我正在使用 python shell 与代理服务器一起测试请求。 在阅读文档 (http://docs.python-requests.org/en/master/user/advanced/) 和一些 stackoverflow 线程后,我正在执行以下操作:

import requests

s = requests.session()
proxies = {'http': 'http://90.178.216.202:3128'}
s.proxies.update(proxies)
req = s.get('http://jsonip.com')

在此之后,如果我打印 req.text,我会得到: u'{"ip":"我当前的IP(不是我之前插入的代理服务器IP)","about":"/about", ......}'

您能否解释一下为什么我得到的是我的计算机的 IP 地址而不是代理服务器的 IP 地址? 我是不是在某个地方出错了,还是我期望在这里发生错误的事情? 我是请求 + 代理服务器的新手,所以我想确保我理解这一点。

更新 我的代码中也有这个:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'}
s.headers.update(headers)

谢谢 维托里奥

【问题讨论】:

    标签: python proxy python-requests ip-address


    【解决方案1】:

    站点 (http://jsonip.com) 广播“Upgrade-Insecure-Requests”标头。这意味着您的请求被重定向到 https://jsonip.com,因此 requests 不使用代理,因为您的 proxies 字典中没有 https 代理。

    所以,你所要做的就是在 proxies 添加一个 https 代理,例如:

    proxies = {'http':'http://90.178.216.202:3128', 'https':'https://90.178.216.202:3128'}
    

    【讨论】:

    • @pajarnas 那你为什么不发布一个问题呢?如果您没有得到任何答案,您可以 ping 我,我会尽力提供帮助。
    【解决方案2】:

    而不是这样做传递用户代理

    requests.post(url='abc.com',header={'user-agent':'Mozila 5.0'})

    【讨论】:

    • 我已经更新了我的问题以包括这个,我之前没有提到过,因为我认为这并不重要
    【解决方案3】:

    您需要更改您的获取请求以使用代理。

    类似这样的:req = s.get('http://jsonip.com', proxies=proxies)

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 1970-01-01
      • 2015-02-21
      • 2011-03-11
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      相关资源
      最近更新 更多