python网络爬虫(二)——数据的清洗与组织

学会了网络爬虫发送请求后,我们可以获得一段目标的HTML代码,但是还没有把数据提取出来,接下来需要进行数据的清洗与组织。

python 复制代码
for item in data:
    result={
        'title':item.get_test(),
        'link':item.get('href')
    }
    print(result)

首先明确要提取的数据是标题和链接,标题在a标签中,提取标签的正文用get_text()方法;链接在a标签的href属性中,提取标签中的href属性用get()方法,在括号中指定要提取的属性数据,即get('href')

需要使用的正则符合如下:

\d:匹配数字

+:匹配前一个字符1次或多次

在Python中调用正则表达式时使用re库,这个库不用安装,可以直接调用。可以用如下代码:

python 复制代码
import requests
import re
from bs4 import BeautifulSoup

url = 'https://www.bilibili.com/video/BV1TC4y1N7dB/?spm_id_from=333.1007.0.0&vd_source=912d1bec97cad7dac820d2ba865f116a'
strhtml = requests.get(url)
#print(strhtml.text)

soup = BeautifulSoup(strhtml.text,'lxml')
data = soup.select('#main >div >div.mtop.firstMod.clearfix>div.centerBox>ul.newsList>li>a')
print(data)

for item in data:
    result={
        'title':item.get_test(),
        'link':item.get('href')
        'ID:'re.findall('\d+',item.get('href'))
    }
    print(result)
相关推荐
zhousenshan15 小时前
Python爬虫常用框架
开发语言·爬虫·python
IMER SIMPLE16 小时前
人工智能-python-深度学习-经典神经网络AlexNet
人工智能·python·深度学习
CodeCraft Studio16 小时前
国产化Word处理组件Spire.DOC教程:使用 Python 将 Markdown 转换为 HTML 的详细教程
python·html·word·markdown·国产化·spire.doc·文档格式转换
皮皮林55116 小时前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
weixin_4569042716 小时前
Spring Boot 用户管理系统
java·spring boot·后端
趁你还年轻_16 小时前
异步编程CompletionService
java
DKPT16 小时前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
sibylyue16 小时前
Guava中常用的工具类
java·guava
奔跑吧邓邓子17 小时前
【Java实战㉞】从0到1:Spring Boot Web开发与接口设计实战
java·spring boot·实战·web开发·接口设计
专注API从业者17 小时前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python