怎么用Python爬取惠农网苹果数据
发表于:2024-10-10 作者:千家信息网编辑
千家信息网最后更新 2024年10月10日,这篇文章将为大家详细讲解有关怎么用Python爬取惠农网苹果数据,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。本次目标爬取惠农网信息受害者地址http
千家信息网最后更新 2024年10月10日怎么用Python爬取惠农网苹果数据
这篇文章将为大家详细讲解有关怎么用Python爬取惠农网苹果数据,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
本次目标
爬取惠农网信息
受害者地址
https://www.cnhnb.com/
环境
Python3.6
pycharm
爬虫代码
导入工具
import requestsimport parselimport csvimport time
请求头
headers = { 'Cookie': 'deviceIdRenew=1; Hm_lvt_91cf34f62b9bedb16460ca36cf192f4c=1604579356,1604659451; deviceId=d1dd5b9-d191-406b-971d-391916a0e; sessionId=S_0KH64T2IHLHSO77N; lmvid=b24dcd0ad2a8f0b783f248c7ff2675a8; lmvid.sig=w1UBnTUKSDq-GfAlx6TyR_K7SjyujGIlF-1kRjTrOAI; hnUserTicket=b80e6b3a-38a3-432c-816d-aeb0376228bd; hnUserId=870048250; Hm_lpvt_91cf34f62b9bedb16460ca36cf192f4c=1604659904', 'Host': 'www.cnhnb.com', 'Referer': 'https://www.cnhnb.com/supply/pingguo/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',}
解析网站、爬取数据
def get_page_url(page_url, page_id): response_2 = requests.get(url=page_url, headers=headers) selector_2 = parsel.Selector(response_2.text) num_id = selector_2.css('#__layout > div > div > div > div > ul > li:nth-child(1) > a::attr(href)').get().split('/')[2] title = selector_2.css('.proinfo-title::text').get().strip() # 标题 update_time = selector_2.css('.update-time::text').get().strip('更新时间:') # 更新时间 price = selector_2.css('.priceTxt .orange .fs30::text').get().strip() + '元' # 价格 ads = selector_2.css('div:nth-child(5) > span.fs14.gray6::text').get() # 发货地址 get_phone_url = 'https://gateway.cnhnb.com/banana/im/operate/wechatcall' data = { 'businessType': '1', 'sourceFrom': '2', 'ticket': '"b80e6b3a-38a3-432c-816d-aeb0376228bd"', 'userId': '{}'.format(num_id), } head = { 'authority': 'gateway.cnhnb.com', 'method': 'POST', 'path': '/banana/im/operate/wechatcall', 'scheme': 'https', 'accept': 'application/json, text/plain, */*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'cache-control': 'no-cache', 'content-length': '98', 'content-type': 'application/json', 'origin': 'https://www.cnhnb.com', 'pragma': 'no-cache', 'referer': 'https://www.cnhnb.com/gongying/{}/'.format(page_id), # 'Cookie': 'deviceIdRenew=1; Hm_lvt_91cf34f62b9bedb16460ca36cf192f4c=1604579356,1604659451; deviceId=d1dd5b9-d191-406b-971d-391916a0e; sessionId=S_0KH64T2IHLHSO77N; lmvid=b24dcd0ad2a8f0b783f248c7ff2675a8; lmvid.sig=w1UBnTUKSDq-GfAlx6TyR_K7SjyujGIlF-1kRjTrOAI; hnUserTicket=b80e6b3a-38a3-432c-816d-aeb0376228bd; hnUserId=870048250; Hm_lpvt_91cf34f62b9bedb16460ca36cf192f4c=1604659904', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-site', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', 'x-b3-traceid': '0KH64WG5WL1GXPNG', 'x-client-appid': '5', 'x-client-id': 'c10e4e9a-5e19-4ba2-a934-c8c5c56680f5', 'x-client-nonce': '62f080cd-ad30-4590-b362-b1c9e660a8d5', 'x-client-page': '/gongying/{}/'.format(page_id), 'x-client-sid': 'S_0KH64W0GT18JX07L', 'x-client-ticket': 'b80e6b3a-38a3-432c-816d-aeb0376228bd', 'x-client-time': '1604659611092', 'x-hn-job': 'If you see these message, I hope you dont hack us, I hope you can join us! Please visit https://www.cnhnkj.com/job.html', } response_3 = requests.post(url=get_phone_url, json=data, headers=head) html_data = response_3.json() if html_data['errorCode'] == 0: dit = { '标题': title, '更新时间': update_time, '价格': price, '发货地址': ads, '联系方式': html_data['data']['messageTitle'] } csv_writer.writerow(dit) print(dit) else: dit = { '标题': title, '更新时间': update_time, '价格': price, '发货地址': ads, '联系方式': '商家设置防打扰' } csv_writer.writerow(dit) print(dit)
保存数据
f = open('惠农网信息.csv', mode='a', encoding='utf-8-sig', newline='')csv_writer = csv.DictWriter(f, fieldnames=['标题', '更新时间', '价格', '发货地址', '联系方式'])csv_writer.writeheader()
效果图
关于怎么用Python爬取惠农网苹果数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
地址
时间
更新
数据
惠农
价格
标题
方式
联系方式
联系
苹果
信息
内容
文章
更多
知识
篇文章
不错
代码
受害者
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
查询数据库字符集
兰州市开明网络技术有限公司
软件开发技术指标参数
水族水书数据库
数据库数组循环结构
吴江网络安全监察大队
北京求索网信网络技术有限公司
中学生网络安全口诀
南宁凶宅数据库
mysql中的数据库模式
普陀区推广网络技术商家
数据库索引字段顺序
云笔记数据用什么数据库
最好的网络安全指数
网络安全日常检查记录表
天津惠普服务器虚拟化费用
数据库白皮书发布
又什么软件可以恢复数据库
mysql 数据库介绍
数据库技术在线测试
intel软件开发手册
中国碳质球粒陨石数据库
游戏再开个服务器占内存吗
北京求索网信网络技术有限公司
国家网络安全战略定义
数据库技术与应用课后答案6
网络安全意识情况调查报告
han数据库取数
深圳深资创软件开发技术
多盟无线网络技术分公司