Scrapy模拟登录赶集网的实现代码
作者:人丑就要多读书-Wu 时间:2023-02-22 20:33:47
1.打开赶集网登录界面,先模拟登录并抓包,获得post请求的request参数
2. 我们只需构造出上面的参数传入formdata即可
参数分析:
setcookie:为自动登录所传的值,不勾选时默认为0。
__hash__值的分析:只需要查看response网页源代码即可 ,然后用正则表达式提取。
3.代码实现
1.workon到自己的虚拟环境 cmd切换到项目目录,输入scrapy startproject ganjiwangdenglu,然后就可以用pycharm打开该目录啦。
2.在pycharm terminal中输入scrapy ganji ganjicom 创建地址,如下为项目目录
3. 代码详情
import scrapy
import re
class GanjiSpider(scrapy.Spider):
name = 'ganji'
allowed_domains = ['ganji.com']
start_urls = ['https://passport.ganji.com/login.php']
def parse(self, response):
hash_code = re.search(r'"__hash__":"(.+)"}', response.text).group(1) # 正则获取哈希
img_url = 'https://passport.ganji.com/ajax.php?dir=captcha&module=login_captcha' # 验证码url
yield scrapy.Request(img_url, callback=self.do_formdata, meta={'hash_code': hash_code}) # 发送获取验证码请求并保存验证码到本地
def do_formdata(self, response):
with open('yzm.jpg', 'wb') as f:
f.write(response.body)
# 验证码三种方案:1,保存下来手动输入,2,云打码,3 tesseract模块,在这里我们手动输入
code = input('请输入验证码:')
# 创建表单
formdata = {
'username': 'your_username',
'password': 'your_password',
'setcookie': '14',
'checkCode': code,
'next': '',
'source': 'passport',
'__hash__': response.request.meta['hash_code'] # meta是在respose.request中
}
login_url = "https://passport.ganji.com/login.php"
yield scrapy.FormRequest(url=login_url, formdata=formdata, callback=self.after_login) # 发送登录请求
def after_login(self, response):
print(response.text)
4.终端输入scrapy carwl ganji 即可大功告成 。
返回来的json字符串解析如下:
注:setting中的设置不在赘述。
来源:https://www.cnblogs.com/bertwu/p/13210539.html
标签:Scrapy,登录,赶集网
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 内置函数速查表一览
2021-10-30 07:43:06
SQL Server 安全检查列表全攻略
2008-01-29 13:31:00
python 实现查找文件并输出满足某一条件的数据项方法
2021-05-10 11:51:30
python中面向对象的注意点概述总结
2023-10-08 09:35:11
![](https://img.aspxhome.com/file/2023/6/81516_0s.jpg)
OpenCV-Python实现油画效果的实例
2022-03-26 09:16:37
![](https://img.aspxhome.com/file/2023/6/101266_0s.jpg)
Python中关于面向对象私有属性方法的详细讲解
2022-11-29 05:38:36
![](https://img.aspxhome.com/file/2023/8/96988_0s.png)
Python Scrapy多页数据爬取实现过程解析
2021-02-28 08:14:01
python实现学员管理系统(面向对象版)
2022-04-08 12:55:32
分析Python的Django框架的运行方式及处理流程
2022-03-13 14:47:50
![](https://img.aspxhome.com/file/2023/9/93759_0s.png)
Python全面解析json数据并保存为csv文件
2021-02-14 03:26:26
ASP中利用正则表达式实现论坛UBB代码转换
2008-02-29 11:49:00
python实现BackPropagation算法
2022-03-26 12:06:39
matplotlib绘制直方图的基本配置(万能模板案例)
2021-04-21 02:35:42
![](https://img.aspxhome.com/file/2023/9/128329_0s.png)
对Python Pexpect 模块的使用说明详解
2022-10-24 02:14:09
简易的全屏透明遮罩(lightBox)
2010-06-09 20:56:00
Python tensorflow与pytorch的浮点运算数如何计算
2023-06-28 14:13:15
![](https://img.aspxhome.com/file/2023/2/95442_0s.jpg)
Django项目中用JS实现加载子页面并传值的方法
2022-07-05 00:18:28
研究Python的ORM框架中的SQLAlchemy库的映射关系
2021-05-23 14:31:07
Python可视化神器pyecharts绘制雷达图
2021-09-30 14:50:48
![](https://img.aspxhome.com/file/2023/3/102953_0s.png)
flask-socketio实现WebSocket的方法
2022-09-25 17:26:24