Urllib请求不在单独的计算机上工作

2024-03-28 17:42:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用fiddler跟踪HTTP请求

这允许我用urllib自动填充表单

它在我正在使用的jupyter笔记本中运行良好,并将其交给一位同事试用。他的电脑坏了

我对这个完全陌生,所以也许我犯了一个简单的错误。我想可能和饼干头有关吧

我正在网上填写姓名和邮政编码

请求:

import urllib.request  as urllib2

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

添加标题:

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")

发送信息:

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()

它没有返回URL或HTTP错误,而是返回包含文本<b>An ERROR has occurred. Please try again. If the issue persists, please try again later.</b>\的HTML

那么,为什么这在我的电脑上工作,而在我的同事身上却不行呢

还有,有没有更好的方法?这看起来很混乱的标题。我有一种感觉,可能有一种更整洁的方式来自动填写表格


Tags: andhttpsaddapplicationresponsebodyurllib2req
1条回答
网友
1楼 · 发布于 2024-03-28 17:42:24

如上所述,问题是cookie中的会话ID。您的同事需要将其替换为自己的会话ID才能正常工作。你应该可以买一个新的

相关问题 更多 >