首页 新闻 会员 周边 捐助

有没有大神能用python模拟登录同程旅游网?

0
悬赏园豆:20 [待解决问题]

我用已经登录成功的cookie去获取同程其它页面获取不了

 1 import re
 2 import requests
 3 import http.cookiejar
 4 from PIL import Image
 5 import time
 6 import json
 7 import urllib
 8 
 9 headers ={'Content-Type':'application/x-www-form-urlencoded',
10            'Host': 'm.ly.com',
11            'Referer': 'https://m.ly.com/passport/login.html?returnUrl=%2fmember%2f',
12            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'
13 }
14 # 建立一个会话,可以把同一用户的不同请求联系起来;直到会话结束都会自动处理cookies
15 session = requests.Session()
16 # 建立LWPCookieJar实例,可以存Set-Cookie3类型的文件。
17 # 而MozillaCookieJar类是存为'/.txt'格式的文件
18 session.cookies = http.cookiejar.LWPCookieJar("cookieTC")
19 # 若本地有cookie则不用再post数据了
20 try:
21     session.cookies.load(ignore_discard=True)
22 except IOError:
23     print('Cookie未加载!')
24 
25 #登录
26 def login(mobilecode, yzm):
27     #输入自己的账号密码,模拟登录同程
28     url = 'https://m.ly.com/passport/login.html?returnUrl=%2fmember%2f'
29     data = {'mobile1':mobilecode,
30         'validCode':yzm,
31         '__RequestVerificationToken':'hZ/SKgQlyt8vwucimmrSE/v0+s122CUtmW7YcU9jfQfwdlgrBIgvq2HOMb58/QRv9inkXvpT5K0nHHVEK15BTtdn7xhzuZFiniRGyKwYLukQdnDj8uNEvjLO29mT19PgC0YABQ==',
32         'returnUrl':'/member/',
33         'mobile':mobilecode,
34             'action':'2'
35                 }
36 
37     # 验证码直接登录
38     result = session.post(url,data,headers=headers)
39 # 打印返回的响应,r = 1代表响应失败,msg里是失败的原因
40 # loads可以反序列化内置数据类型,而load可以从文件读取
41     if (result.status_code==200):
42      print('登录成功!')
43 # 保存cookie到本地
44     #session.cookies='NewProvinceId=25; NCid=321; 17uCNRefId=RefId=6928722&SEFrom=baidu&SEKeyWords=; TicketSEInfo=RefId=6928722&SEFrom=baidu&SEKeyWords=; CNSEInfo=RefId=6928722&tcbdkeyid=&SEFrom=baidu&SEKeyWords=&RefUrl=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3D67G4miqOtaoF97NtX5uN2RzagsAweQ6N0ce3W4UspX3%26wd%3D%26eqid%3Dde89f82300021e66000000045a4aff5f; ASP.NET_SessionId=0nzduagmnmpafxocsujy4jh2; nus=userid=213466735&nickName=%e5%90%8c%e7%a8%8b%e4%bc%9a%e5%91%98_5ACD74728FC&level=1; usLoginType=1; Hm_lvt_64941895c0a12a3bdeb5b07863a52466=1513845651,1514864489; Hm_lpvt_64941895c0a12a3bdeb5b07863a52466=1514865100; qdid=39264|1|6928722|0a6c16; cnUser=token=&nickName=&loginType=; us=userid=&nickName=&level=; token=; loginRecord=name=&pass=; passport_login_state=pageurl=https%3a%2f%2fmember.ly.com%2forder; Identifier=w_l_s=d3cf095561cd4af1bba8bd143754af64&a_l_s=d45a87fe49ef4d29a938472d8cf1853c&ssid=0nzduagmnmpafxocsujy4jh2; _dx_uzZo5y=mk8m0xSsfkJyXBEhm60t3W7U5ZX7Vwg; __tctmc=144323752.196439725; __tctmd=144323752.258115222; __tctma=144323752.1513845659711168.1513845659008.1514864487867.1514872492280.5; __tctmb=144323752.113205166748855.1514872492280.1514872494674.2; __tctmu=144323752.0.0; __tctmz=144323752.1514864487867.4.1.utmccn=(organic)|utmcmd=organic|utmEsl=gb2312|utmcsr=baidu|utmctr=; longKey=1513845659711168; __tctrack=0; Hm_lvt_c6a93e2a75a5b1ef9fb5d4553a2226e5=1513845666,1514864498; Hm_lpvt_c6a93e2a75a5b1ef9fb5d4553a2226e5=1514872496; route=1aebb3c4227d40c9df356a037f3f28ca; td_sid=MTUxNDg3MjUwNywzNzM5ODVmYjU3NzkxYjE2MjFmMDE3NzQwOGQ3MWIzMTAxNDEyYjJkYTUwMWQ5NzE2OTE1MGYyZmI0Yzc1YjZlLDM1ODMxMTA5YjQ5MmEwNzFkOTgyMWFmZWFhMjVhYmFlMDIwN2FmMWFmYWFiY2JiZGY3OTlmNjI1ZWFkY2M1Mjg=; k_st=101.81.69.135|1514872507; td_did=Kn5A1OHzKaGHD4TPDkzIRP2PnD5xxruSKSLS1kf8OYoEChDx4GHi6DvxiBa1fWP%2B34lXhWuQjVB2ce7evb1RKpgDWhxlevhmmQWUAb8JksGmuYG4GlbpSxlBBPBAddPT68dUpKKFN0eoUzcPC6PQkHr2YybgNawRgrl7JNR0NnnspPIglG1ZtjzysjEiDKqaimzEreflInSjBGiaTThygg%3D%3D; t_q=1514872507855'
45     session.cookies.save(ignore_discard=True, ignore_expires=True)
46 
47 #验证登录
48 def isLogin(mobilecode,yzm):
49     data = {'action': 'MobileLogin',
50             'name': mobilecode,
51             'mobilecode': yzm,
52             'v': '0.953055684627679'
53             }
54     # 通过查看用户个人信息来判断是否已经登录
55     url = "https://member.ly.com/order"
56     # 禁止重定向,否则登录失败重定向到首页也是响应200
57     login_code = session.post(url, data=data,headers=headers,allow_redirects=False).status_code
58     if login_code == 200:
59         return True
60     else:
61         return False
62 
63 
64 #爬取我的订单
65 def open_url(mobilecode,yzm):
66     data = {'action': 'MobileLogin',
67             'name': mobilecode,
68             'mobilecode': yzm,
69             'v': '0.953055684627679'
70             }
71     goodsURL = 'https://member.ly.com/order'
72     content = session.post(goodsURL,data=data,headers=headers).text
73     return content.text
74 
75 if __name__ == '__main__':
76     mobilecode = input('请输入手机号:')
77     yzm = input('请输入验证码:')
78     if isLogin(mobilecode,yzm): #验证登录
79         print('登录成功')
80         print(open_url(mobilecode,yzm)) #爬取订单
81 
82     else:
83         login(mobilecode, yzm) #登录同程
Abraham2017的主页 Abraham2017 | 初学一级 | 园豆:182
提问于:2018-01-02 16:15
< >
分享
所有回答(2)
0

这种事情只能是慢慢检查,不要以为得到一个cookie就ok了,可能有更多的cookie或者其他参数。

关于怎么检查:直接的方式是网页先到达你需要爬的页面,把请求拿出来分析对比,看看缺哪些参数(不一定就是cookie)。

粗略看了一下你的code,同城居然没验证码? —— 如果有,验证码的流程通常如下,先到某uri Get,获取cookie(session),然后使用该cookie 去下一个uri    取验证码(图片),然后图像识别,然后才是post 登录。如果是大规模登录还要注意策略...

花飘水流兮 | 园豆:13615 (专家六级) | 2018-01-03 09:43

我是选择手机验证码登录的,登录成功后保存成功cookie,再用cookie(session.post)去获取订单页面,可问题就是获取不到

支持(0) 反对(0) Abraham2017 | 园豆:182 (初学一级) | 2018-01-03 10:48
0

关键问题不是登录,是cookie能不能用.

你直接复制网页上的cookie可以访问到订单页面吗?你复制的cookie和你登录保存下来的参数一样吗?

考虑这些问题

Masako | 园豆:1893 (小虾三级) | 2018-01-05 14:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册