Python3爬虫爬取百姓网列表并保存为json功能示例【基于request、lxml和json模块】
作者:包子源 时间:2023-12-16 21:28:44
本文实例讲述了Python3爬虫爬取百姓网列表并保存为json功能。分享给大家供大家参考,具体如下:
python3爬虫之爬取百姓网列表并保存为json文件。这几天一直在学习使用python3爬取数据,今天记录一下,代码很简单很容易上手。
首先需要安装python3。如果还没有安装,可参考本站python3安装与配置相关文章。
首先需要安装requests和lxml和json三个模块
需要手动创建d.json文件
代码
import requests
from lxml import etree
import json
#构造头文件,模拟浏览器访问
url="http://xian.baixing.com/meirongfuwu/"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36','referer':url}
response=requests.get(url,headers=headers)
body=response.text #获取网页内容
html=etree.HTML(body,etree.HTMLParser())
gethtml=html.xpath('//div[contains(@class,"media-body-title")]')
# 存储为数组list
jsondata = []
for item in gethtml:
jsonone={}
jsonone['title']=item.xpath('.//a[contains(@class,"ad-title")]/text()')[0]
jsonone['url']=item.xpath('.//a[contains(@class,"ad-title")]/attribute::href')[0]
jsonone['phone']=item.xpath('.//button[contains(@class,"contact-button")]/attribute::data-contact')[0]
jsondata.append(jsonone)
# 保存为json
with open("./d.json",'w',encoding='utf-8') as json_file:
json.dump(jsondata,json_file,ensure_ascii=False)
结果
PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat
在线json压缩/转义工具:
http://tools.jb51.net/code/json_yasuo_trans
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/ziwoods/article/details/84257645
标签:Python3,爬虫,json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JS原生轮播图的简单实现(推荐)
2024-04-29 14:06:30
基于PHP的加载类操作以及其他两种魔术方法的应用实例
2024-05-11 09:55:18
人工智能学习pyTorch的ResNet残差模块示例详解
2022-05-04 21:45:22
![](https://img.aspxhome.com/file/2023/8/82778_0s.jpg)
Python OpenCV超详细讲解调整大小与图像操作的实现
2022-07-09 21:16:27
![](https://img.aspxhome.com/file/2023/2/105582_0s.png)
python使用socket创建tcp服务器和客户端
2021-04-21 21:10:17
uniapp实现支付功能
2023-08-21 13:53:47
![](https://img.aspxhome.com/file/2023/4/55964_0s.png)
运用Python巧妙处理Word文档的方法详解
2023-11-13 16:58:29
![](https://img.aspxhome.com/file/2023/3/104923_0s.jpg)
解决pytorch 的state_dict()拷贝问题
2022-10-05 22:03:57
详解利用python识别图片中的条码(pyzbar)及条码图片矫正和增强
2022-12-26 18:01:16
![](https://img.aspxhome.com/file/2023/0/112100_0s.png)
使用Python对Dicom文件进行读取与写入的实现
2022-05-30 12:18:27
![](https://img.aspxhome.com/file/2023/4/135304_0s.jpg)
chatGPT deBug解决管理员登入服务器返回401问题
2022-08-07 06:38:09
![](https://img.aspxhome.com/file/2023/6/131806_0s.png)
基于vue-resource jsonp跨域问题的解决方法
2023-07-02 16:33:44
![](https://img.aspxhome.com/file/2023/8/139708_0s.jpg)
Mysql自动备份与还原方法
2024-01-15 03:19:32
python 基本结构语句(函数和模块)
2023-06-14 00:37:56
![](https://img.aspxhome.com/file/2023/5/85175_0s.png)
用python求一个数组的和与平均值的实现方法
2021-01-10 20:11:24
Python实现从url中提取域名的几种方法
2022-03-14 12:55:30
分布式爬虫scrapy-redis的实战踩坑记录
2022-03-02 02:13:24
![](https://img.aspxhome.com/file/2023/9/120969_0s.png)
用Python实现命令行闹钟脚本实例
2023-01-19 12:57:09
Python中函数带括号和不带括号的区别及说明
2023-01-23 05:23:28
![](https://img.aspxhome.com/file/2023/3/105253_0s.png)
javascript模拟鼠标自动点击链接
2007-11-03 19:08:00