Sending form data to aspx page

2024/9/25 9:38:48

There is a need to do a search on the website

    url = r'http://www.cpso.on.ca/docsearch/'

this is an aspx page (I'm beginning this trek as of yesterday, sorry for noob questions)

using BeautifulSoup, I can get the __VIEWSTATE and __EVENTVALIDATION like this:

    viewstate = soup.find('input', {'id' : '__VIEWSTATE'})['value']eventval = soup.find('input', {'id' : '__EVENTVALIDATION'})['value']

and the header can be set like this:

    headers = {'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13',
'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8',
'Content-Type': 'application/x-www-form-urlencoded'}

if you go to the webpage, the only values I really want to pass are the first name and last name...

    LN = "smith"FN = "a"data = {"__VIEWSTATE":viewstate,"__EVENTVALIDATION":ev,"ctl00$ContentPlaceHolder1$MainContentControl1$ctl00$txtLastName":LN, "ctl00$ContentPlaceHolder1$MainContentControl1$ctl00$txtFirstName":FN}

so putting it all together its like this:

    import urllibimport urllib2import urlparseimport BeautifulSoupurl = r'http://www.cpso.on.ca/docsearch/'html = urllib2.urlopen(url).read()soup = BeautifulSoup.BeautifulSoup(html)viewstate = soup.find('input', {'id' : '__VIEWSTATE'})['value']ev = soup.find('input', {'id' : '__EVENTVALIDATION'})['value']headers = {'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13','HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8','Content-Type': 'application/x-www-form-urlencoded'}LN = "smith"FN = "a"data = {"__VIEWSTATE":viewstate,"__EVENTVALIDATION":ev,"ctl00$ContentPlaceHolder1$MainContentControl1$ctl00$txtLastName":LN, "ctl00$ContentPlaceHolder1$MainContentControl1$ctl00$txtFirstName":FN}data = urllib.urlencode(data)request = urllib2.Request(url,data,headers)response = urllib2.urlopen(request)newsoup = BeautifulSoup.BeautifulSoup(response)for i in newsoup:print i

The problem is that it doesnt really seem to give me the results... don't know if I need to supply every value for every textbox in the form or what... maybe I'm just not doing it properly. anyways, just hoping someone could set me straight. I thought I had it but i would expect to see a list of doctors and contact info.

any insight is much appreciated, I have used beautifulsoup before, but I think my problem is just sending Request and having the right amount of info in the data part.

Thanks!

Answer

took advice from @pguardiario and went the mechanize route... much simpler

    import mechanizeurl = r'http://www.cpso.on.ca/docsearch/'request = mechanize.Request(url)response = mechanize.urlopen(request)forms = mechanize.ParseResponse(response, backwards_compat=False)response.close()form = forms[0]form['ctl00$ContentPlaceHolder1$MainContentControl1$ctl00$txtLastName']='Smith'form['ctl00$ContentPlaceHolder1$MainContentControl1$ctl00$txtPostalCode']='K1H'print mechanize.urlopen(form.click()).read()

I am a long way from finishing, but this is getting me a lot further.

https://en.xdnf.cn/q/71591.html

Related Q&A

What is the difference between imregionalmax() of matlab and scipy.ndimage.filters.maximum_filter

I need to find the regional maxima of an image to obtain foreground markers for watershed segmentation. I see in matlab use the function imregionalmax(). As I dont have the matlab software, I use the f…

crontab: python script being run but does not execute OS Commands

I have this crontab configuration setup and the following script.MAILTO="[email protected]" 41 15 * * * /usr/bin/python /home/atweb/Documents/opengrok/setup_and_restart.py > /home/at…

concatenating arrays in python like matlab without knowing the size of the output array

I am trying to concatenate arrays in python similar to matlab array1= zeros(3,500); array2=ones(3,700); array=[array1, array2];I did the following in python:array1=np.zeros((3,500)) array2=np.ones((3,7…

How to save the result of a comparison using Djangos with template tag?

I would like to create new variable in django template, which will have a value of comparison obj.site.profile.default_role == objUnfortunately none of this code works: {% with obj.site.profile.default…

How to merge two list of dictionaries based on a value

I have two lists of dictionaries, lets say: a = [{id: 1, name: a}] b = [{id: 1, city: b}]I want to have a list that merges every dictionary in both lists with the same ID. In this example i expect to h…

How can I format strings to query with mysqldb in Python?

How do I do this correctly:I want to do a query like this:query = """SELECT * FROM sometable order by %s %s limit %s, %s;""" conn = app_globals.pool.connection() cur = con…

Doing many iterations of curve_fit in one go for piecewise function

Im trying to perform what are many iterations of Scipys curve_fit at once in order to avoid loops and therefore increase speed.This is very similar to this problem, which was solved. However, the fact …

python - dictionary iterator for pool map

I am handling set of frozensets. I am trying to find minimal sets for each frozenset in the dictionary output. I have 70k frozensets, so i am making chunk of this frozenset dictionary and parallelizing…

How to get SciPy.integrate.odeint to stop when path is closed?

edit: Its been five years, has SciPy.integrate.odeint learned to stop yet?The script below integrates magnetic field lines around closed paths and stops when it returns to original value within some t…

High-dimensional data structure in Python

What is best way to store and analyze high-dimensional date in python? I like Pandas DataFrame and Panel where I can easily manipulate the axis. Now I have a hyper-cube (dim >=4) of data. I have be…