Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - make a POST request using Python 3 urllib

I am trying to make a POST request to the following page: http://search.cpsa.ca/PhysicianSearch

In order to simulate clicking the 'Search' button without filling out any of the form, which adds data to the page. I got the POST header information by clicking on the button while looking at the network tab in Chrome Developer Tools. The reason I'm posting this instead of just copying solutions from the other similar problems is that I believe I may have not gotten the correct header information.

Is it properly formatted and did I grab the right information? I've never made a POST request before.

This is what I've managed to piece together:

import urllib.parse import urllib.request   data = urllib.parse.urlencode({'Host': 'search.cpsa.ca', 'Connection': 'keep-alive', 'Content-Length': 23796,                                      'Origin': 'http://search.cpsa.ca', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',                                      'Cahce-Control': 'no-cache', 'X-Requested-With': 'XMLHttpRequest',                                      'X-MicrosoftAjax': 'Delta=true', 'Accept': '*/*',                                      'Referer': 'http://search.cpsa.ca/PhysicianSearch',                                      'Accept-Encoding': 'gzip, deflate',                                      'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6',                                      'Cookie': 'ASP.NET_SessionId=kcwsgio3dchqjmyjtwue402c; _ga=GA1.2.412607756.1459536682; _gat=1'})   url = "http://www.musi-cal.com/cgi-bin/query?%s"  data = data.encode('ascii') with urllib.request.urlopen("http://search.cpsa.ca/PhysicianSearch", data) as f:     print(f.read().decode('utf-8')) 

This solution outputs the page's HTML, but not with any of the data I wanted to retrieve from the POST request.

like image 588
Daniel Paczuski Bak Avatar asked Apr 07 '16 18:04

Daniel Paczuski Bak


People also ask

How do I request a post using Urllib?

To make a basic request in Python 3, you will need to import the urllib. request module, this contains the function urlopen() which you can use to make a request to a specified URL. To fetch the actual output of the request, you can use the read() function on the returned object to read the contents.

How do you create a POST request in Python?

To create a POST request in Python, use the requests. post() method. The requests post() method accepts URL. data, json, and args as arguments and sends a POST request to a specified URL.

Is Urllib built in Python 3?

The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.


1 Answers

This is how you do it.

from urllib import request, parse data = parse.urlencode(<your data dict>).encode() req =  request.Request(<your url>, data=data) # this will make the method "POST" resp = request.urlopen(req) 
like image 83
C Panda Avatar answered Sep 21 '22 22:09

C Panda