Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Urllib请求不在单独的计算机上工作_Python_Http_Urllib_Fiddler - Fatal编程技术网

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

Python Urllib请求不在单独的计算机上工作,python,http,urllib,fiddler,Python,Http,Urllib,Fiddler,我正在使用跟踪HTTP请求 这使我能够使用urllib自动填写表单 它在我使用的jupyter笔记本中运行良好,并交给一位同事试用。这在他的电脑上不起作用 我对这个完全陌生,所以也许我犯了一个简单的错误。我想可能跟饼干头有关吧 我在一张在线表格中填写姓名和zipcode 请求: import urllib.request as urllib2 req = urllib2.Request("https://carlowcoco.checktheregister.ie/publicpages/R

我正在使用跟踪HTTP请求

这使我能够使用urllib自动填写表单

它在我使用的jupyter笔记本中运行良好,并交给一位同事试用。这在他的电脑上不起作用

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

我在一张在线表格中填写姓名和zipcode

请求:

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错误,而是返回包含文本
的HTML。发生了错误。请再试一次。如果问题仍然存在,请稍后再试。\

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


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

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

你的cookie中有会话ID,我猜这就是issue@SuperStew我该怎么处理它呢?是否应该删除它?这可能是网站正常工作所必需的,但他可能需要用自己的会话ID替换它。@supersew我认为这可能是问题所在。我在登录页上添加了一行来执行
get
,这将获得一个新的会话ID。然后在运行请求之前将其添加到标题信息中。现在好像有用了。如果您想添加一个答案,比如“尝试获取一个新会话ID”,那么我会将其标记为answe.rCool,added answer