当前位置:网站首页>爬取国家法律法规数据库
爬取国家法律法规数据库
2022-06-27 17:31:00 【墨子】
Part1实验目的
现如今大数据时代已经到来,网络爬虫技术成为这个时代不可或缺的一部分。爬虫的目标对象也很丰富,不论是文字、图片、视频,任何结构化非结构化的数据爬虫都可以爬取。企业需要数据来分析用户行为,来分析自己产品的不足之处,来分析竞争对手的信息等等。个人通过Spyder爬虫软件,爬取我们所需要的信息,我们可以通过爬虫提前获取一些信息,如:我们可以通过爬虫进行抢票,抢课,获取考研调剂信息等等。本文以爬取国家法律法规的数据库数据为例。
本文作者:江西农业大学 经济管理学院 金融1903傅艳
Part2实验步骤
1观察网页
通过观察网页发现,进入法律法规条文,网址没有发生变化,说名该网页是动态网页。


2请求网页
我们在浏览器右键点击“检查”,点击“网络”,在出现的界面中选择“Fetch/XHR”按钮,并刷新页面。通过Ctrl+F查找到有关内容,点击出现的链接,我们点击“预览”,发现了我们需要的标题、制定机关、法律性质、时效性、公布日期等信息都在这里,点击“标头”则可以看到网页地址的真实地址。 

3尝试获取第一页的信息
使用requests请求数据库,请求方法是get,


我们查看“标头”发现请求方法为get请求,查看“负载”并点击,即为get请求的参数。Request请求代码如下:
import requests
items=[]
url='https://flk.npc.gov.cn/api/?type=flfg&xlwj=05&searchType=title%3Baccurate&sortTr=f_bbrq_s%3Bdesc&gbrqStart=&gbrqEnd=&sxrqStart=&sxrqEnd=&sort=true&page=1&size=10&_=1654157294070'
r=requests.get(url)
4解析数据,存储数据
由于网页返回的是json格式数据,获取我们需要的标题、制定机关、法律性质、时效性、公布日期,我们通过字典访问即可。其中每条法规的网页链接如何拿到?我们点击第一条法规,发现其网址后缀存储在url里,就可以拿到法规详细界面的完整链接。


先嵌入字典解析库,通过访问字典,一层一层将数据提取得到一页的所有信息,编辑代码:
json=r.json()
pagelist=json['result']['data']
office=page['office']
title=page['title']
type=page['type']
date=page['publish']
page=[url]
5通过循环,爬取所有页面的法规数据
翻页爬取的关键是找到真实地址的“翻页”规律。我们分别点击第1页、第2页、第3页,发现不同页码的除了page参数不一致,其余相同。第1页的“page”是1,第2页的“page”是2,第2页的“page”是2,以此类推。 
我们嵌套一个For循环,并通过pandas as pd存储数据。运行代码让其自动爬取1-11的法规信息,并储存在666661.csv的文件中,所有代码如下:
items=[]
url='https://flk.npc.gov.cn/api/?type=flfg&xlwj=05&searchType=title%3Baccurate&sortTr=f_bbrq_s%3Bdesc&gbrqStart=&gbrqEnd=&sxrqStart=&sxrqEnd=&sort=true&page=1&size=10&_=1654157294070'
for i in range(1,11):
form_data={'page': 1,
'size': 10}
r=requests.get(url)
json=r.json()
pagelist=json['result']['data']
for page in pagelist:
office=page['office']
title=page['title']
type=page['type']
date=page['publish']
page=[url]
item=[title,office,type,date,page]
items.append(item)
import pandas as pd
df=pd.DataFrame(items)
df.to_csv('666661.csv',encoding='utf-8-sig')
最后,得到爬取的数据结果如下:

边栏推荐
- Market status and development prospect forecast of the global infusion needle less connector industry in 2022
- Vs code runs "yarn run dev" and reports "yarn": the file XXX cannot be loaded
- Camera calibration with OpenCV
- Usage of rxjs mergemap
- 基于STM32F103ZET6库函数外部中断实验
- Space calculation of information and innovation industry in 2022
- 你知道 log4j2 各项配置的全部含义吗?带你了解 log4j2 的全部组件
- Industry university cooperation cooperates to educate people, and Kirin software cooperates with Nankai University to complete the practical course of software testing and maintenance
- 华大单片机KEIL添加ST-LINK解决方法
- 在arcgis中以txt格式导出点的坐标
猜你喜欢

External interrupt experiment based on stm32f103zet6 library function

金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东

Vscode suggests that you enable gopls. What exactly is it?

利用OpenCV执行相机校准

云原生数据库:数据库的风口,你也可以起飞

Minmei new energy rushes to Shenzhen Stock Exchange: the annual accounts receivable exceeds 600million and the proposed fund-raising is 450million

Function key input experiment based on stm32f103zet6 Library

Technology sharing | introduction to kubernetes pod

openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题

Good news - British software 2022 has obtained 10 invention patents!
随机推荐
国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)
Tupu digital twin intelligent energy integrated management and control platform
Using WebDAV instead of 445 port file share
Exporting coordinates of points in TXT format in ArcGIS
Current market situation and development prospect forecast of global 3,3 ', 4,4' - biphenyltetracarboxylic dianhydride industry in 2022
Cucumber自动化测试框架使用
International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding
Is Guosen Securities a state-owned enterprise? Is it safe to open an account with Guosen Securities?
Add in address of idea official website
电脑安全证书错误怎么处理比较好
Introduction to deep learning and neural networks
“我让这个世界更酷”2022华清远见研发产品发布会圆满成功
VS code 运行yarn run dev 报yarn : 无法加载文件XXX的问题
教你打印自己的日志 -- 如何自定义 log4j2 各组件
NVIDIA Clara-AGX-Developer-Kit installation
External interrupt experiment based on stm32f103zet6 library function
Space calculation of information and innovation industry in 2022
【云驻共创】高校数字化差旅建设“解决之道”
PostgreSQL数据库WAL——资源管理器RMGR
The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder