【发布时间】:2015-06-07 11:02:33
【问题描述】:
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()发送时怎么办?
【问题讨论】:
-
您在尝试使用
requests.post(url, proxies=proxies, auth=auth, data=data)时遇到了什么问题 -
您确定这里的问题是代理身份验证吗?
-
是的,代理身份验证是这里真正的问题。 @VikasNehaOjha 使用它会给出错误“TypeError:post() 最多接受 3 个参数(给定 4 个)”。所以不能这样用。
-
好的。试试这个 -
proxies = {'http': 'http://username:password@ip:port', 'https': 'http://username:password@ip:port'}然后requests.post(url, proxies=proxies, data=data) -
成功了。非常感谢,这真的很有帮助。
标签: python proxy beautifulsoup http-post http-get