当前位置:网站首页>半自动化爬虫-爬取一个网站的内容及回复
半自动化爬虫-爬取一个网站的内容及回复
2022-08-01 16:42:00 【爱学习的Amelia】
- 需求:爬取某个帖子的内容以及回复。
- 步骤:
- 首先到该页面存储好该页面的源代码。右击该页面,选择【查看该页面源代码】,将得到的页面中的文本全部复制到一个文本文档中。
- 将文件存储好之后,修改好需要得到的一些值的正则表达式,将文件路径改好,运行以下代码。
注意:正则表达式一定要根据需要获取数据的需要来进行编写。多个空格可以使用\\s+来匹配。
import re
import csv
with open('./data/半自动化爬虫-抗压背锅吧.txt','r',encoding='utf-8') as f:
source=f.read()
result_list=[]
username_list=re.findall('username="(.*?)"',source,re.S)
content_list=re.findall('class="d_post_content j_d_post_content " style="display:;">\\s+(.*?)<',source, re.S)
reply_time_list=re.findall('class="tail-info">(2022.*?)<',source, re.S)
for i in range(len(username_list)):
result={
'username': username_list[i],
'content': content_list[i],
'reply_time': reply_time_list[i]}
result_list.append(result)
with open('半自动化爬虫-抗压背锅吧.csv','w',encoding='utf-8') as f:
writer=csv.DictWriter(f,fieldnames=['username','content','reply_time'])
writer.writeheader()
writer.writerows(result_list)
- 最终得到的结果是一个表格,由于有些部分是图片,所以提取不出来,这里只能提取出文字部分。
- 修改后的代码
import re
import csv
with open('./data/半自动化爬虫-抗压背锅吧.txt','r',encoding='utf-8') as f:
source=f.read()
# 获得包含每一层所有信息的大块文本
every_reply=re.findall('class="l_post l_post_bright j_l_post clearfix "(.*?)p_props_tail props_appraise_wrap',source, re.S)
# 从每一个大文本快里面提取出各个楼层的发帖人姓名,发帖时间和发帖内容
for each in every_reply:
result={
}
result['username'] = re.findall('username="(.*?)"',source,re.S)[0]
result['content'] = re.findall('class="d_post_content j_d_post_content " style="display:;">\\s+(.*?)<',source, re.S)
result['reply_time'] = re.findall('class="tail-info">(2022.*?)<',source, re.S)
with open('半自动化爬虫-抗压背锅吧1.csv','w',encoding='utf-8') as f:
writer=csv.DictWriter(f,fieldnames=['username','content','reply_time'])
writer.writeheader()
writer.writerows(result_list)
边栏推荐
- 使用Canvas实现网页鼠标签名效果
- ESP8266-Arduino编程实例-GA1A12S202对数刻度模拟光传感器
- Live tonight!
- 02 es cluster construction
- 全新升级!《云原生架构白皮书 2022 版》重磅发布
- Ant discloses the open source layout of core basic software technology for the first time
- 如何有效地开发 Jmix 扩展组件
- PAT 甲级 A1003 Emergency
- Good guy, the company server just crashed!
- C#的DataTable帮助类
猜你喜欢

MySQL最大建议行数2000w, 靠谱吗?

华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
MySQL INTERVAL 关键字指南

Bugku-Misc-贝斯手

短剧正在抢长剧的生意

04 flink cluster construction

1个月写900多条用例,2线城市年薪33W+的测试经理能有多卷?

直播app开发,是优化直播体验不得不关注的两大指标

七夕专属博文-使用QGraphics画“红心“或“黑心“(含数学模型讲解)

ESP8266-Arduino编程实例-74HC595位移寄存驱动
随机推荐
moxa串口服务器配置说明(moxa串口驱动)
金仓数据库 KingbaseES V8.3 至 V8.6 迁移最佳实践(4. V8.3 到 V8.6 数据库移植实战)
The untiy Resources directory dynamically loads resources
zabbix部署和简单使用
08 Spark cluster construction
2022年深圳市促进大健康产业集群高质量发展的若干措施
个人日记
ODrive开发 #1 ODrive固件开发指南[通俗易懂]
Winform的UI帮助类——部分组件会使用到DevExpress组件
GridControl helper class for DevExpress
经验|如何做好业务测试?
02 es 集群搭建
怎么安装汉化包(svn中文语言包安装)
京东软件测试面试题,仅30题就已经拯救了50%的人
夸克网盘资源站
中国驻西班牙使馆再次提醒留学人员注意暑期安全
Financial products with high annualized returns
【paper】Cam2BEV论文浅析
使用Canvas 实现手机端签名
Winform message prompt box helper class