Python请求失败,500错误

时间:2015-11-16 23:17:45

标签: c# python python-requests

我在GitHub上使用这个优秀的项目(https://github.com/cjyoung/MouseBitesWPF)。但是,这是用C#编写的,我真的需要用Python编写的东西。我把这些代码简化为它需要工作的绝对骨架,并提出了这个:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DisneyFinderSmaller
{
    class Program
    {

        static CookieContainer cookieJar = new CookieContainer();
        static internal string rootUrl = "https://disneyworld.disney.go.com";
        static internal string siteUrl = "/dining/";        
        static internal string diningSearchUrl = "/finder/dining-availability";


        static void Main(string[] args)
        {
            LaunchSearchInstance();
            string test = Console.ReadLine();

        }


        private static void LaunchSearchInstance()
        {
            string result = getCookiesFromRequest(rootUrl + siteUrl, "", "GET");
            string pep_csrf = "";
            Match match = Regex.Match(result, "<input[^>]*name=['\"]pep_csrf['\"][^>]*value=['\"]([^'\"]*)['\"]*[^>]>", RegexOptions.Singleline & RegexOptions.IgnoreCase);
            pep_csrf = match.Groups[1].ToString();
            ConductSearch(pep_csrf);
        }

        private static void ConductSearch(string pep_csrf)
        {


            string postString = string.Format("&searchDate={1}" +
                                                "&skipPricing=true" +
                                                "&searchTime={2}" +
                                                "&partySize={3}" +
                                                "&id={0}%3BentityType%3Drestaurant" +
                                                "&type=dining" +
                                                "&pep_csrf={4}",
                                                   "293704",
                                                    "2015-11-18",
                                                    "80000714",
                                                    "2",
                                                    pep_csrf);

            string result = getCookiesFromRequest(rootUrl + diningSearchUrl, postString, "POST");
            System.Console.WriteLine(result);

        }

        private static String getCookiesFromRequest(string url, string postString, string method = "POST")
        {
            String result = "";
            byte[] postBytes = Encoding.ASCII.GetBytes(postString);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.Referer = rootUrl + siteUrl;
            request.CookieContainer = cookieJar;
            if (method == "POST")
            {
                request.ContentType = "application/x-www-form-urlencoded";
                Stream postStream = request.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Close();
            }
            try
            {

                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
                Stream responseStream = webResponse.GetResponseStream();
                StreamReader responseStreamReader = new StreamReader(responseStream);
                result = responseStreamReader.ReadToEnd();
                responseStream.Close();
                webResponse.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("IOException source: {0}", ex.Source);
            }
            return result;
        }
     }
}

在我使用Requests将其转换为Python的努力中,我想出了这个:

#!/usr/bin/env python

import requests


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


session = requests.Session()

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

raw = "&searchDate=2015-11-18&skipPricing=true&searchTime=80000714&partySize=2&id=293704%3BentityType%3Drestaurant&type=dining&pep_csrf=" +  pep

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'referer': 'https://disneyworld.disney.go.com/dining/',
    'method' : 'POST'   
}
result = session.post(url2, data=raw, headers=headers)

print result.status_code

但这不起作用并返回500的状态代码。

有关事情出错的任何想法?我一直把头靠在墙上几天,任何见解都会非常感激。

0 个答案:

没有答案