使用Python请求从西南方向请求数据时出现超时错误

时间:2018-04-25 23:15:15

标签: python python-requests

import threading
from lxml import html
from bs4 import BeautifulSoup
import time
import datetime 
import requests 


def worker(oa,da,ods):
    """thread worker function"""
    print (oa + ' ' + da + ' ' + ods + ' ' +  str(datetime.datetime.now()))
    url = "https://www.southwest.com"
    xurl = "https://www.southwest.com/api/air-booking/v1/air-booking/page/air/booking/shopping"
    rh = {
    'accept': 'application/json,text/javascript,*/*;q=0.01',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.5',
    'cache-control': 'max-age=0',
    'content-length': '454',
    'Connection' : 'keep-alive',
    'content-type': 'application/json',
    'origin':'https://www.southwest.com',
    'Referer': 'https://www.southwest.com/air/booking/select.html?originationAirportCode=MCI&destinationAirportCode=SJC&returnAirportCode=&departureDate=2018-04-26&departureTimeOfDay=ALL_DAY&returnDate=&returnTimeOfDay=ALL_DAY&adultPassengersCount=1&seniorPassengersCount=0&fareType=USD&passengerType=ADULT&tripType=oneway&promoCode=&reset=true&redirectToVision=true&int=HOMEQBOMAIR&leapfrogRequest=true',
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
    'Authorization' : 'null null',
    'X-Api-IDToken' : 'null',
    'X-API-Key': 'l7xx944d175ea25f4b9c903a583ea82a1c4c',
    'X-Channel-ID': 'southwest',
    'X-User-Experience-ID': '7dced3e7-6667-47ff-8f19-cb1df5935eed'
    }


    ps = {
    'adultPassengersCount': '1',
    'application'   :'air-booking',
    'departureDate' :'2018-05-28',
    'departureTimeOfDay'    :'ALL_DAY',
    'destinationAirportCode': 'MCO',
    'fareType'  :'USD',
    'int'   :'HOMEQBOMAIR',
    'leapfrogRequest': 'true',
    'originationAirportCode':   'MCI',
    'passengerType' :'ADULT',
    'promoCode' : '',
    'redirectToVision'  : 'true',
    'reset' : 'true',
    'returnAirportCode': '',
    'returnDate': '',
    'returnTimeOfDay':  'ALL_DAY',
    'seniorPassengersCount' : '0',
    'site'  : 'southwest',
    'tripType'  : 'oneway'
    }

   # with requests.Session() as s:
    #r = requests.post(url,headers = rh ,params = ps)
#        soup = BeautifulSoup(r.content,'html.parser')
#        soup = BeautifulSoup(r.content,'lxml')
    #print(r)
    #print(r.content)
    s = requests.Session()
    r = s.get(url)
    #print(r.content)
    r = s.post(xurl,headers = rh,params = ps, timeout = 10)
    print(r.content)    
    print(ps)
    print(rh)
    print (oa + ' ' + da + ' ' + ods + ' ' + str(datetime.datetime.now()))

    return


#db = MySQLdb.connect(host="localhost",user="root",passwd="",db="")
rcount = 0

tdelta = 55
#print(strt_date)
threads = []
count = 1 
thr_max = 2
r = ["MCI","DEN","MCI","MDW","MCI","DAL"]

strt_date = (datetime.date.today() + datetime.timedelta(days=tdelta)).strftime("%m/%d/%Y")

while count < 2:
    t = threading.Thread(name=r[count-1]+r[count],target=worker,args=(r[count-1],r[count],strt_date))
    threads.append(t)
    t.start()
    count = count + 2

请告知我在这里做错了什么,因为我收到了超时错误。 我尝试了使用get,post的不同方法,但它没有帮助。

我得到了标题&amp;来自开发者控制台的网络选项卡的参数。

我还可以尝试成功获取搜索结果吗? 使用post / get in请求之间的根本区别是什么?

1 个答案:

答案 0 :(得分:0)

我错过了标题中的一些参数。修改标题后,它现在正常工作。