Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use requests.post() with proxy authentication in python?

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

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?

like image 470
kenway Avatar asked Dec 02 '22 14:12

kenway


1 Answers

On behalf of Vikas Neha Ojha so all can see

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

and then

requests.post(url, proxies=proxies, data=data)
like image 193
Shaggy89 Avatar answered Dec 06 '22 11:12

Shaggy89