Python+Requests+Xpath实现动态参数获取实战
1、古诗文网直接登录时,用浏览器F12抓取登录接口的入参,我们可以看到框起来的key对应的value是动态参数生成的,需获取到;

2、登录接口入参的值一般是登录接口返回的原数据值,若刷新后接口与对应源码(element)的值存在一个为空一个有值,那么久看下是否存在ajax请求,再获取动态参数的值

3、我们获取动态参数的值,使用到etree中的xpath进行解析
from TestCase.Api_Review.ClassCode import Chaojiying_Client
from lxml import etree
import requests
import os
s = requests.Session()
# 新建文件夹
if not os.path.exists('./gushiwenLibs'):
os.makedirs('./gushiwenLibs')
# 对验证码图片进行抓捕及识别
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
}
page_text = s.get(url=url,headers=headers,proxies=None).text
tree = etree.HTML(page_text)
img_url = "https://so.gushiwen.cn/RandCode.ashx"+tree.xpath('//*[@id="imgCode"]/@src')[0]
__VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]
__VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]
4、登录界面的图片验证码,我们先获取对应的图形验证码,下载到本地,然后再使用第三方平台进行提取
参考此链接:Python+Request库+第三方平台实现验证码识别示例
img_src = s.get(url=img_url,headers=headers).content
# 图片存储的路径
fileName = './gushiwenLibs/'+'code_img_data.jpg'
with open(fileName, 'wb') as fp:
fp.write(img_src)
# 使用超级鹰平台实现验证码识别
chaojiying = Chaojiying_Client('TeacherTao', 'TeacherTao', '96001')
with open(fileName, 'rb') as fp:
img = fp.read()
result = chaojiying.PostPic(img, 1004)['pic_str']
# print(result)
5、最后再使用登录接口发起请求,我们使用Session进行登录的,因为请求头中携带Cookies进行登录了
# 登录Url
url_login = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
data = {
'__VIEWSTATE': __VIEWSTATE,
'__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR,
'from': 'http://so.gushiwen.cn/user/collect.aspx',
'email': '18126248212',
'pwd': 'qqq123',
'code': result,
'denglu': '登录',
}
post_text = s.post(url_login,data=data,headers=headers)
# print(post_text.text)
fileName1 = './gushiwenLibs/'+'gushiren.html'
with open(fileName1, 'w',encoding='utf-8') as fp:
fp.write(post_text.text)
6、整个项目的源码:
from TestCase.Api_Review.ClassCode import Chaojiying_Client
from lxml import etree
import requests
import os
s = requests.Session()
# 新建文件夹
if not os.path.exists('./gushiwenLibs'):
os.makedirs('./gushiwenLibs')
# 对验证码图片进行抓捕及识别
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
}
page_text = s.get(url=url,headers=headers,proxies=None).text
tree = etree.HTML(page_text)
img_url = "https://so.gushiwen.cn/RandCode.ashx"+tree.xpath('//*[@id="imgCode"]/@src')[0]
__VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]
__VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]
img_src = s.get(url=img_url,headers=headers).content
# 图片存储的路径
fileName = './gushiwenLibs/'+'code_img_data.jpg'
with open(fileName, 'wb') as fp:
fp.write(img_src)
# 使用超级鹰平台实现验证码识别
chaojiying = Chaojiying_Client('TeacherTao', 'TeacherTao', '96001')
with open(fileName, 'rb') as fp:
img = fp.read()
result = chaojiying.PostPic(img, 1004)['pic_str']
print(result)
# 登录Url
url_login = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
data = {
'__VIEWSTATE': __VIEWSTATE,
'__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR,
'from': 'http://so.gushiwen.cn/user/collect.aspx',
'email': '账号',
'pwd': '密码',
'code': result,
'denglu': '登录',
}
post_text = s.post(url_login,data=data,headers=headers)
# print(post_text.text)
fileName1 = './gushiwenLibs/'+'gushiren.html'
with open(fileName1, 'w',encoding='utf-8') as fp:
fp.write(post_text.text)
Python+Requests+Xpath实现动态参数获取实战的更多相关文章
- Python+requests 发送简单请求--》获取响应状态--》获取请求响应数据
Python+requests 发送简单请求-->获取响应状态-->获取请求响应数据 1.环境:安装了Python和vscode编译器(Python自带的编译器也ok).fiddler抓包 ...
- 【学习笔记】--- 老男孩学Python,day10, 函数, 动态参数 命名空间\作用域 global nonlocal
1. 动态参数 位置参数的动态参数: *args 关键字参数的动态参数 : **kwargs 顺序:位置---*args---默认值---**kwargs 在形参上*聚合, **聚合 在实参上*打散, ...
- python函数知识二 动态参数、函数的注释、名称空间、函数的嵌套、global,nonlocal
6.函数的动态参数 *args,**kwargs:能接受动态的位置参数和动态的关键字参数 *args -- tuple *kwargs -- dict 动态参数优先级:位置参数 > 动态位置参数 ...
- python requests + xpath 获取分页详情页数据存入到txt文件中
直接代码,如有不懂请加群讨论# *-* coding:utf-8 *-* #import jsonimport requestsimport pytesseractimport timeimport ...
- Python+Requests+Xpath(解析)爬取某站点简历图片(数据分析三)
1.环境安装 pip install lxml 2.解析原理 使用通用爬虫爬取网页数据 实例化etree对象,且将页面数据加载到该对象中 使用xpath函数结合xpath表达式进行标签定位和指定数据提 ...
- python+requests传两种参数体
在JMeter请求参数中,我们了解到,在做接口测试时,发送请求的参数有两种格式,一种是Parameters,一种是JSON.怎么区分请看 https://www.cnblogs.com/testlea ...
- 跟着太白老师学python 10day 函数的动态参数 *args, **kwargs, 形参的位置顺序
1. *args 接收实参的位置参数, **kwargs接收实参的关键字参数 def func(*args, **kwargs): print(args, kwargs) func(1, 2, 3, ...
- python 函数动态参数,名称空间,global,nonlocal
##################################总结######################################动态参数 *args:位置参数动态传参,接收到的是元 ...
- python 函数 动态参数 和嵌套
1.动态参数 是可以接收任意的参数.一种方式, 1,位置的动态传参, 写法是: *参数名 接收的参数是tuple类型举个例子:def yue(*food): print(food)yue(" ...
随机推荐
- 三维视觉惯性SLAM的有效Schmidt-EKF
三维视觉惯性SLAM的有效Schmidt-EKF An Efficient Schmidt-EKF for 3D Visual-Inertial SLAM 论文地址: http://openaccess ...
- 使用BootstrapVue相关组件,构建Vue项目界面
基于Vue的前端框架有很多,Element算一个,而BootstrapVue也可以非常不错的一个,毕竟Bootstrap也是CSS中的大佬级别的,它和Vue的整合,使得开发起来更加方便了.Bootst ...
- 我的N年软件测试感悟
1.前言 大家好!我是Meng前段时间,很荣幸被一合作伙伴邀请发表一篇文章,主题为"这些年,我所从事软件测试的一些感悟",正好趁着这个机会,我也好好总结一下. 2.测试培训 对于软 ...
- jsp页面抽取
步骤: 1.先将jsp中要抽取的公共部分剪切出来,黏贴到新的jsp文件中,取名叫xxx.jsp 2.在需要引入此公共部分的jsp页面中使用<%@include file="xxx.js ...
- 【NX二次开发】Block UI 组
设置组及组内成员不可见 this->group->GetProperties()->SetLogical("Show", false); 设置组及组内成员不可操作 ...
- 【NX二次开发】NX内部函数,pskernel.dll文件中的内部函数
pskernel.dll文件中的内部函数,含有部分pk函数,用法可以查看pk函数帮助: ADPAPE ADVXED APPTRA ATGETO ATTGEO BLECHK BLECRB BLECVR ...
- 基于Ubuntu下以Docker方式gitlab软件的部署
基于Ubuntu下以Docker方式gitlab软件的部署 目录 基于Ubuntu下以Docker方式gitlab软件的部署 1.安装Docker Compose 1.1 下载curl 1.2 安装c ...
- Oracle数据泵导出数据库
Oracle数据泵导出数据库 特别注意:如果后续要导入的数据库版本低,所有导出命令就需要在后面加一个version=指定版本. 例如从11g导出数据导入到10g,假设10g具体版本为10.2.0.1, ...
- UnityBug之KeyStore
UnityException: Can not sign the applicationUnable to sign the application; please provide passwords ...
- LevelDB学习笔记 (2): 整体概览与读写实现细节
1. leveldb整体介绍 首先leveldb的数据是存储在磁盘上的.采用LSM-Tree实现,LSM-Tree把对于磁盘的随机写操作转换成了顺序写操作.这是得益于此leveldb的写操作非常快,为 ...