12306 验证码 爬取

本文是 12306 验证码 的爬取 ,由于同学在做 12306 验证码 识别,我就顺手写了一个爬虫来爬取验证码。=v=

几点说明

由于12306过快刷新会提示稍后再来,所以我就调用time.sleep 休眠一秒。

还有需要注意的是,由于12306的https证书坑爹,所以用requests的话,需要设置verify = False

Code

使用方法:在相应的savePath 建立相应的路径。然后运行代码即可。比如我的路径是E:\12306\,提前建好文件夹即可。。。

# -*- coding:utf-8 -*-
import time
import requests

def download_file(url,local_filename):
    r = requests.get(url, stream=True,verify = False)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
                f.flush()
    return local_filename

savePath = 'E://12306/'
url = 'https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&0.7923694306518883'
for i in xrange(0x7fffffff):
    print download_file(url, savePath + str(i+1) +'.jpg')
    time.sleep(1)     #休眠一秒

 

Python爬虫系列教程

python 爬虫 入门

python模拟登陆

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > 12306 验证码 爬取
本文地址:https://www.hrwhisper.me/crawling-12306-verification-code/

听说长得好看的已经打赏了

python learning , . permalink.

2 thoughts on “12306 验证码 爬取

Leave a Reply

Your email address will not be published. Required fields are marked *