3
我试图将弹出窗体的数据发布到本地网站。为此,我正在模拟网站提供的请求标题,数据和Cookie信息。 (注:我主要是从代码中修改我的电子邮件和密码(出于显而易见的原因),但所有其他代码将保持不变。)Python请求创建Cookie失败
我尝试了cookie,头文件,请求,数据等的多个排列组合此外,我已经在网络督察中验证了cookie和预期的标题和数据。我可以使用requests' sample code轻松设置cookie。我无法解释为什么我的代码无法在现场使用,我非常感谢任何帮助。请参阅以下代码以获取更多详细信息。
import requests
import robobrowser
import json
br = robobrowser.RoboBrowser(user_agent="Windows Chrome",history=True)
url = "http://posting.cityweekly.net/gyrobase/API/Login/CookieV2"
data ={"passwordChallengeResponse":"....._SYGwbDLkSyU5gYKGg",
"email": "<email>%40bu.edu",
"ttl":"129600",
"sessionOnly": "1"
}
headers = {
"Origin": "http://posting.cityweekly.net",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.8,ru;q=0.6",
"User-Agent": "Windows Chrome", #"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
"Referer": "http://posting.cityweekly.net/utah/Events/AddEvent",
"X-Requested-With": "XMLHttpRequest",
"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"Host":"posting.cityweekly.net"
}
cookie = {"Cookie": "__utma=25975215.1299783561.1416894918.1416894918.1416897574.2; __utmc=25975215; __utmz=25975215.1416894918.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __qca=P0-2083194243-1416894918675; __gads=ID=e3b24038c9228b00:T=1416894918:S=ALNI_MY7ewizuxK0oISnqPJWlLDAeKFMmw; _cb_ls=1; _chartbeat2=D6vh2H_ZbNJDycc-t.1416894962025.1416897589974.1; __utmb=25975215.3.10.1416897574; __utmt=1"}
r = br.session.get(url, data=json.dumps(data), cookies=cookie, headers=headers)
print r.headers
print [item for item in r.cookies.__dict__.items()]
请注意,我打印了Cookie对象,并且cookie属性(字典)为空。
欢迎来到StackOverflow。您的答案已经在“非常低质量的帖子评论”队列中结束,因为它看起来像是一个要求澄清而不是实际答案的评论。但是,我可以看到您提供了实际的答案:他们需要使用POST。您可能想要更改您的答案,使其看起来不像问题或评论,以免有人不小心删除它。 – GreenAsJade 2014-12-26 04:03:25