隐藏页面特效

民事案例爬取1

爬取网页:中国法院网 https://www.chinacourt.org/index.shtml

代码如下:(数据连接部分自行修改)

import pymysql import requests import csv from bs4 import BeautifulSoup pnum = 7 while pnum <=50: news_list = [] head = ['新闻标题'] url = 'https://www.chinacourt.org/article/index/id/MzAwNDAwMgCRhAEA/page/'+str(pnum)+'.shtml' headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'} res = requests.get(url,headers=headers) #本来demo这一段是没有的,但是常规解码跑出来是乱码,这里用了暴力解码 demo = res.text.encode("utf-8").decode("utf-8") bs = BeautifulSoup(demo,'html.parser') # 首先爬取这个板块的两个头条 # 为了方便查看,设置了爬取计数变量a和b a = 1 econ = bs.find('div', class_='list').find_all('li') #print(econ) for info in econ: print('正在爬取第{}条案例'.format(a)) news_title = info.find('span', class_='left').find('a')['title'] print(news_title) news_url = info.find('span', class_='left').find('a')['href'] print(news_url) news_time = info.find('span', class_='right').text print(news_time) res2 = requests.get('https://www.chinacourt.org/'+news_url, headers=headers) # 这里也用了暴力解码 demo = res2.text.encode("utf-8").decode("utf-8") bs2 = BeautifulSoup(demo, 'html.parser') paper = bs2.find('div', class_='detail_txt').find_all('p')[0].text+bs2.find('div', class_='detail_txt').find_all('p')[1].text paper = paper.replace(u'\u3000', u'') # 1.连接数据库 conn = pymysql.connect( host='localhost', user='root', password='lin0613', # 密码 db='cus', # 数据库名 charset='utf8', ) # 2.创建游标对象 cur = conn.cursor() # 3.对数据库进行CRUD操作 # try: insert_sqli = "insert into case1 values('"+news_time+"','"+news_title+"','"+paper+"');" cur.execute(insert_sqli) except Exception as e: print("插入数据失败:", e) else: conn.commit() print("插入数据成功;") # 4. 关闭游标 cur.close() # 5. 关闭连接 conn.close() dict_news = {'案例标题': news_title, '时间': news_time, '主要内容':paper} #print(dict_news) news_list.append(dict_news) a += 1 pnum = pnum +1

 


__EOF__

本文作者往心。
本文链接https://www.cnblogs.com/lx06/p/15688408.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   往心。  阅读(60)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示