SCool
SCool

Reputation: 3375

Urllib requests not working on separate computer

I am using fiddler to track HTTP requests.

This allows me to automate filling in a form with urllib.

It was working fine in a jupyter notebook I was using, and gave it to a colleague to try out. It does not work on his computer.

I am completely new to this, so perhaps there's a simple mistake I'm making. I think it might have something to do with the cookies header maybe?

I am filling in a name, surname and zipcode into an online form.

The request:

import urllib.request  as urllib2

req = urllib2.Request("https://carlowcoco.checktheregister.ie/publicpages/Results.aspx")

Adding Headers:

req.add_header("Connection", "keep-alive")
req.add_header("Cache-Control", "max-age=0")
req.add_header("Origin", "https://carlowcoco.checktheregister.ie")
req.add_header("Upgrade-Insecure-Requests", "1")
req.add_header("Content-Type", "application/x-www-form-urlencoded")
req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.116")
req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
req.add_header("Referer", "https://carlowcoco.checktheregister.ie/publicpages/ereg.aspx?CID=4&uiLang=en-GB")
req.add_header("Accept-Encoding", "gzip, deflate, br")
req.add_header("Accept-Language", "en-US,en;q=0.9")
req.add_header("Cookie", "_ga=GA1.2.1485303330.1563803355; _fbp=fb.1.1563803355623.389471504; _gid=GA1.2.1242949638.1567500110; ASP.NET_SessionId_eReg=wbyf1iuvothtmdr0zxq4ypnv; _gat=1")

Sending the information:

firstname='john'
lastname='smith'
zipcode='abc123'

# this is where we add the name, surname and zipcode
body = f"__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTExODA1MzM2NzFkZI2Y9Vj1N4c71dOJShLXen0Q8nT0&__VIEWSTATEGENERATOR=1627BCCD&__PREVIOUSPAGE=o3Y5pVByrKh5ylQa3zb19RrpXCBCTakCQLkYw24qRyH07uZC4V8-00fT-aZjmROM9Gnkny1RyjaEBGfxfBR95RnY9Dn0zJEhObiGTquHfVvYnOZx0&__EVENTVALIDATION=%2FwEWBwKFwaWxBQLp48u6DgK95LDpBAK62djbDgLthcGDBQL0mu%2BYCwK83r2cAZJf50Jf%2F9CI7cXegRb5oL0hvtD1&ctl00%24MainContent%24TextBoxPostcode={zipcode}&ctl00%24MainContent%24TextBoxFirstName={firstname}&ctl00%24MainContent%24TextBoxSurname={surname}&ctl00%24MainContent%24FormSubmit=Submit"

# convert to bytes object
body = body.encode('utf-8')

# send request and save to response
response = urllib2.urlopen(req, body)

# read response and convert to string
page = response.read()

It's not returning a URL or HTTP Error, it's returning HTML which contains the text <b>An ERROR has occurred. Please try again. If the issue persists, please try again later.</b>\.

So why does this work on my computer, but not my colleagues?

Also, is there a better method of doing this? This looks very messy with the headers. I have a feeling there's probably a tidier way to automate form filling.

Upvotes: 0

Views: 140

Answers (1)

SuperStew
SuperStew

Reputation: 3054

As mentioned above, the issue is the session ID in the cookie. Your colleague will need to replace that with his own session ID for it to work. You should be able to GET a new one.

Upvotes: 1

Related Questions