如何在python中使用带有代理身份验证的requests.post()? [英] how to use requests.post() with proxy authentication in python?

查看:656
本文介绍了如何在python中使用带有代理身份验证的requests.post()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()发送某些数据时该怎么办?

This code works fine when I use requests.get(url, proxies=proxies, auth=auth), but what to do when there is some data that has to be sent through requests.post() under proxy authentication?

推荐答案

代表Vikas Neha Ojha,所有人都能看到

On behalf of Vikas Neha Ojha so all can see

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

然后

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

这篇关于如何在python中使用带有代理身份验证的requests.post()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆