簡體   English   中英

如何更改request.Session()對象的代理?

[英]How do you change the proxy of a requests.Session() Object?

總體背景:

我有一個代理列表,並且有一個PDF url列表。 我正在下載這些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

通過更改代理,我不必重新登錄該站點,對嗎?

只需列出代理列表,然后循環瀏覽

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日

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM