鱼C论坛

 找回密码
 立即注册
查看: 725|回复: 4

爬取igola机票遇到session has expired

[复制链接]
发表于 2019-6-9 11:30:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这两天爬取一个机票查询的网站  发现python已经和浏览器的请求基本一致了,在最后一步获取关键的机票json数据时,提示The session has expired
不知道哪里出错了,各位有空可以一起找找问题所在
代码不长,可以看看
session_url = 'https://www.igola.com/web-gateway/api-flight-polling-data-hub/create-session'
polling_url = 'https://www.igola.com/web-gateway/api-flight-polling-data-hub/packagedPolling'
# t = datetime.now().timestamp()
# timestamp = 97 * t % 1000 + t * 1000
spayload = json.dumps({'lang': 'ZH',
                       'enableMagic': 'true',
                       'magicEnabled': 'true',
                       'adultAmount': 1,
                       'childAmount': 0,
                       'queryObj': {'cabinAlert': 'false',
                                    'cabinType': 'Economy',
                                    'isDomesticCabinType': 0,
                                    'item': [{'date': '20190614',
                                              'from': {'c': 'CAN', 't': 'C'},
                                              'to': {'c': 'TPE', 't': 'C'}},
                                             # {'date': returnDateCode,
                                             #  'from': {'c': arrivalCode, 't': 'C'},
                                             #  'to': {'c': departCode, 't': 'C'}}
                                             ],
                                    'passengerInfo': [],
                                    # 'tripType': 'RT'}}).replace(' ', '')
                                    'tripType': 'OW'}}).replace(' ', '')
with requests.session() as s:
    # sheaders
    timestamp=str(int(time.time() * 1000))
    s.headers = {'Accept': 'application/json, text/plain, */*',
                 # 'Accept-encoding': 'gzip, deflate, br',
                 'Accept-Language': 'ZH',
                 'authorization': 'null',
                 # 'connection': 'keep-alive',
                 # 'content-length': str(len(spayload)),
                 'Content-Type': 'application/json;charset=UTF-8',
                 'DNT': '1',
                 'guid': 'null',
                 'igola-client': 'pc',
                 'Origin': 'https://www.igola.com',
                 'Referer': 'https://www.igola.com/flights/timeline/ZH-CNY-1-OW-Economy-0-0?trip=can-tpe&date=2019-06-14',
                 'timestamp': timestamp,
                 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',

                 }
    print('请求头1')
    print(s.headers)
    print('payload1:')
    print(spayload)
    sr = s.post(session_url, data=spayload,verify=False)
    print('response1:')
    print(sr.headers)
    resultCode = sr.json()['resultCode']
    print(resultCode)
    if resultCode != 200:
        print('状态码1: %s' % resultCode)
        return s.cookies, s.headers
    sessionid = sr.json()['sessionId']
    print(sessionid)
    # packagedPolling请求
    payload = json.dumps(
        {"currency": "CNY",
         "lang": "ZH",
         "sorters": [{"sortType": "score", "sortOrder": "des", "voyage": 0}],
         "filters": [],
         "pageNumber": 1,
         "pageSize": 30,
         "sessionId": sessionid,
         "needHiding": 'true'}
    ).replace(' ', '')
    # s.headers['content-length'] = str(len(payload))
    s.headers['Session-Id'] = sessionid
    # s.headers['timestamp'] = str(int(time.time() * 1000))
    # s.headers['authority'] = 'www.igola.com'
    # s.headers['method'] ='POST'
    # s.headers['path'] = '/web-gateway/api-flight-polling-data-hub/separatedPolling'
    # s.headers['scheme'] ='https'
    print('请求头2:')
    print(s.headers)
    print('payload2:')
    print(payload)
    r = s.post(polling_url, data=payload,verify=False)
    print('response2:')
    print(r.text)
    print(r.headers)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-6-9 18:02:18 | 显示全部楼层
没人嘛。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-9 22:03:16 | 显示全部楼层
意思就是session过期呗
网页没看 大概2个思路 模拟登陆/直接复制cookie进headers
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-10 14:25:05 From FishC Mobile | 显示全部楼层
ietar 发表于 2019-6-9 22:03
意思就是session过期呗
网页没看 大概2个思路 模拟登陆/直接复制cookie进headers

用浏览器试过,不需要cookie就能获取数据的,应该不是cookie的问题,目前在想是不是有东西在js里面,有空再去试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-10 15:23:13 | 显示全部楼层
没细看 , 不过 明显的 会话过期 .
1. selenium 模拟登录
2. 复制 cookie 进 headers
3. 上面 方法 二选一
4. 浏览器直接输入 URL 也会自动生成一个 cookie
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-16 07:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表