全文都是Python知识干货,没有一句多余的废话!
1.Python爬虫原理
网络爬虫:自动的按照我们制定的规则,下载互联网上网络资源的程序。
爬虫的本质:伪造网络请求(90%是http请求)
用途:搜索引擎,数据采集,测试分析
狭义定义:伪造浏览器行为的程序!
2.浏览器工作流程
本质上:浏览器就是一个客户端,它通过http请求从服务器下载数据,解析渲染
展示给用户。
任何一个网页都是n个http请求回的数据组成。
我们在浏览器中看到的所有的信息都是从服务器下载而来。
3.http请求的分析
视频教程+
1.请求头
url method,header
2.请求体
参数
抓包
谷歌浏览器 开发者 调试工具
顺序方法:手动的操作一遍流程,记录所有的http请求
分析http请求,没有捷径,只有耐心,细心和经验。
会话技术
业务流程:
cookie保持
1.访问登录页面
url = 'https://kyfw.12306.cn/otn/login/init'
method = get
2.下载验证码
url = 'https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.785280601210562'
method = get
3.校验验证码
url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
method = post
表单数据:
answer: 183,51,40,116
login_site: E
rand: sjrand
4.校验用户名密码
url = 'https://kyfw.12306.cn/passport/web/login'
method = post
表单数据:
username: wcfdehao
password: 123456
appid: otn
5.获取权限token
url = 'https://kyfw.12306.cn/passport/web/auth/uamtk'
method = post
表单数据:
appid: otn
6.获取权限
url = https://kyfw.12306.cn/otn/uamauthclient
method = post
表单数据:
tk: 依赖上一次请求的数据
4.requests库的介绍
安装:pip install requests
python3 pycharm
5.文件操作
相关命令看Python基础手册即可。
6.Python模拟登陆12306相关步骤
第一步 :访问登录页面
第二步:下载验证码
第三步:校验验证码
第四步:校验用户名和密码
第五步:获取权限token
第六步:获取权限