如何在python中使用requests.post()和代理身份验证?

时间:2015-06-07 11:02:34

标签: python proxy beautifulsoup http-post http-get

from bs4 import BeautifulSoup
import requests
from requests.auth import HTTPProxyAuth

url = "http://www.transtats.bts.gov/Data_Elements.aspx?Data=2" 
proxies = {"http":"xxx.xxx.x.xxx: port"}
auth = HTTPProxyAuth("username", "password")
r = requests.get(url, proxies=proxies, auth=auth)
soup = BeautifulSoup(r.text,"html.parser") 
viewstate_element = soup.find(id = "__VIEWSTATE").attrs 
viewstate = viewstate_element["value"]
eventvalidation_element = soup.find(id="__EVENTVALIDATION").attrs
eventvalidation = eventvalidation_element["value"]


data =     {'AirportList':"BOS",'CarrierList':"VX",'Submit':'Submit',"__EVENTTARGET":"","__EVENTARGUMENT":"","__EVENTVALIDATION":eventvalidation,"}
r = requests.post(url, proxies, auth, data )
print r

当我使用requests.get(url, proxies=proxies, auth=auth)时,此代码可以正常工作,但是如果有一些数据必须通过代理身份验证通过requests.post()发送,该怎么办?

1 个答案:

答案 0 :(得分:10)

代表Vikas Neha Ojha所有人都可以看到

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

然后

requests.post(url, proxies=proxies, data=data)