使用Cookie和身份验证通过请求发送POST

时间:2015-11-13 15:51:15

标签: python cookies python-requests

我正在尝试编写一个简单的脚本来检查迪士尼世界特定餐厅的可用性。我已经跟踪了网站的活动,发现它需要发送一个带有一些特定细节的POST,我认为某种形式的身份验证。我已经运行并返回结果的代码,但它不是我正在寻找的结果。返回似乎是重定向到迪士尼主页面,而不是我要求的查询。有谁知道我在这个程序中缺少什么?

我目前的策略是开始一个带有请求的会话,然后从网站上初步获取,然后我捕获cookie和后来在帖子中使用的这个标识符(pep_csrf)。这一切似乎都很好,但是当我真正做到这一点时,我并没有得到我期待的数据。

import requests
import urllib

url = "https://disneyworld.disney.go.com/dining/"
url2 = "https://disneyworld.disney.go.com/dining/finder/dining-availability"

session = requests.Session()
session.headers.update({'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'})

tokenRequest = session.get(url)
start = tokenRequest.content.find('''id="pep_csrf"''')
pep = tokenRequest.content[start+21:tokenRequest.content.find('>',start+22)-1]
sessionCookies = tokenRequest.cookies


payload = {
'pep_csrf': pep,
'searchDate': '2015-11-13',
'skipPricing': 'true',
'searchTime':'80000714',
'partySize':'2',
'id':'293704;entityType=restaurant',
'type':'dining'
}

raw = urllib.urlencode(payload)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'referer': 'https://disneyworld.disney.go.com/dining/',

}
result = session.post(url2, data=raw, cookies=sessionCookies,   headers=headers)

print result.content

0 个答案:

没有答案
相关问题