当前位置:网站首页>半自动化爬虫-爬取一个网站的内容及回复
半自动化爬虫-爬取一个网站的内容及回复
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)
边栏推荐
- Go unit tests
- PHP security flaws: session hijacking, cross-site scripting, SQL injection and how to fix them
- Using Canvas to achieve web page mouse signature effect
- 05 Doris cluster construction
- 京东软件测试面试题,仅30题就已经拯救了50%的人
- Rancher 部署 DataKit 最佳实践
- The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
- 08 Spark cluster construction
- MLX90640 红外热成像仪测温模块开发笔记(完整版)
- Vulnhub target drone: HARRYPOTTER_ NAGINI
猜你喜欢

时序数据库在船舶风险管理领域的应用

【黑马早报】胡军代言悟空理财涉嫌欺诈,疑似涉及390亿元;涪陵榨菜回应榨菜吃出脚指甲; 鸿星尔克再捐1个亿;腾讯控股股价跌回5年前...

酷逼了 Pathetic Dog 第 304 场周赛

ECCV 2022 | Poseur:你以为我是姿态估计,其实是目标检测哒

Bugku-Misc-贝斯手

90后的焦虑,被菜市场治好了

Live tonight!

沈腾拯救暑期档

Daily Yuxian Big Defeat

Ant discloses the open source layout of core basic software technology for the first time
随机推荐
我的新书销量1万册了!
C # Excel helper classes
Winform message prompt box helper class
SQL函数 TIMESTAMPDIFF
面对营销难,有米云指出一条破局之路
如何防止重复下单?
七夕到了——属于程序员的浪漫
70后夫妻给苹果华为做“雨衣”,三年进账7.91亿
二分练习题
Synchronized原理
Rancher 部署 DataKit 最佳实践
软件测试谈薪技巧:同为测试人员,为什么有人5K,有人 20K?
C# CSV format file helper class
04 flink cluster construction
untiy Resorces目录动态加载资源
聊下自己转型测试开发的历程
OneFlow源码解析:Op、Kernel与解释器
今晚直播!
如何有效地开发 Jmix 扩展组件
C#的DataTable帮助类