Python2 爬虫 登录 12306
Python2 爬虫 登录 12306
阿豪boy 发表于6个月前
Python2 爬虫 登录 12306
  • 发表于 6个月前
  • 阅读 17
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

 需要手动输入验证码

# coding=utf-8

import urllib
import urllib2
import urlparse
import re
import bs4
import requests
import cookielib

import sys
import ssl

reload(sys)
sys.setdefaultencoding('utf-8')

# 关闭证书验证
ssl._create_default_https_context = ssl._create_unverified_context

# 创建cookie
c = cookielib.LWPCookieJar()
# 绑定到urllib2的处理
cookie = urllib2.HTTPCookieProcessor(c)

# 附带cookie提交
openr = urllib2.build_opener(cookie)

url = 'https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.49959081844167086'
req = urllib2.Request(url)
req.add_header('User-Agent',
               'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.3397.16 Safari/537.36')
req.add_header('Referer', 'https://kyfw.12306.cn/otn/login/init')
codeImg = openr.open(req).read()
with open('codeimg.jpg', 'wb') as f:
    f.write(codeImg)

'''
    验证码验证记得以原始尺寸打开,竖直x,横y
'''

codeans = raw_input("请输入验证码:\n")

'''
必须先获取cookie,不然报错:
验证码校验失败,信息为空
'''

data = {
    'answer': codeans,
    'login_site': 'E',
    'rand': 'sjrand'
}

print type(data)
print data
# 将字典转为字符串
data = urllib.urlencode(data)
print type(data)
print data

'''
<type 'dict'>
{'answer': '238,36,128,43', 'rand': 'sjrand', 'login_site': 'E'}
<type 'str'>
answer=238%2C36%2C128%2C43&rand=sjrand&login_site=E
'''
url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
req = urllib2.Request(url)
req.add_header('User-Agent',
               'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.3397.16 Safari/537.36')
html = openr.open(req, data=data).read()
print html




'''
登陆
'''
uid = ''
pwd = ''
url = 'https://kyfw.12306.cn/passport/web/login'

req=urllib2.Request(url)
req.add_header('User-Agent',
               'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.3397.16 Safari/537.36')

req.add_header('Referer', 'https://kyfw.12306.cn/otn/login/init')

data = {
    'username': uid,
    'password': pwd,
    'appid': 'otn'
}

data = urllib.urlencode(data)
html  = openr.open(req,data=data).read()
print html

 

请输入验证码:
180,50,110,110
<type 'dict'>
{'answer': '180,50,110,110', 'rand': 'sjrand', 'login_site': 'E'}
<type 'str'>
answer=180%2C50%2C110%2C110&rand=sjrand&login_site=E
{"result_message":"验证码校验成功","result_code":"4"}
{"result_message":"登录成功","result_code":0,"uamtk":"5p5dsjV2NzYD0IsqCsVjCyxTkdAMVwRMsV-98XGGPhcPaNfpub5150"}

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 10
博文 634
码字总数 480730
×
阿豪boy
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: