【问题标题】:How do you change the proxy of a requests.Session() Object?如何更改 requests.Session() 对象的代理?
【发布时间】:2017-11-29 20:47:46
【问题描述】:

一般背景:

我有一个代理列表,还有一个 PDF 网址列表。我正在下载这些 PDF。

我希望能够每下载几次就切换代理。

我在几个答案中看到了以下内容,但是所有代理都一次使用吗?或者它是从代理的字典中随机出现的?如何选择要使用的代理?

proxies = {
    'https': 'http://username:password@ip:port', 
    'https': 'http://usernamepassword@ip:port',
    'https': 'http://usernamepassword@ip:port',
    'https': 'http://usernamepassword@ip:port',
    'https': 'http://usernamepassword@ip:port',
    'https': 'http://usernamepassword@ip:port'
}

这是我当前代码的示例示例

我的代码:

s = requests.Session()
data = {"Username":"usr", "Password":"psw"}
url = "https://someSite.com"
#Logging into the site
s.post(url, data=data) #add proxies=proxies here?

for download_url in PDFLinks:
    temp = s.get(download_url).content

我有一个可用代理服务器列表

https_proxy_list = "https://IP:port", "https://IP:port", "https://IP:port"

如何更改 request.Session() 对象的代理?对于 POST 和 GET

通过更改代理,我不必重新登录网站,对吧?

【问题讨论】:

    标签: python python-2.7 proxy python-requests


    【解决方案1】:

    只需要一个代理列表,然后循环浏览它们

    s = requests.Session()
    
    proxyList = ['Just imagine there are a few proxies here']
    
    for item in proxyList:
        r2 = s.get(login_url, proxies = {'https' : item}, verify=False)
    
    
        print r2.status_code
        if r2.status_code == 200:
            print "It worked"
            usable_IP.append(item)
    
    
        print usable_IP
    print usable_IP
    

    这是我目前正在使用的代码,它解决了我遇到的问题。 2017 年 12 月 13 日

    【讨论】:

      猜你喜欢
      • 2012-03-17
      • 1970-01-01
      • 2020-05-08
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      相关资源
      最近更新 更多