python html 解析的一些写法

python 复制代码
import requests
from lxml import etree
# 1. 请求网页
url = '链接'
res = requests.get(url)
# 2. 解析网页内容
tree = etree.HTML(res.text)
# 3. 提取数据
ls = tree.xpath('//dl[@class="textList"]//a')
for i in ls:
# 基于当前标签对象获取文本及属性
    name = i.xpath('./text()')[0]
    detail_url = i.xpath('./@href')[0]
    
    下面是获取到内容页后得到页面数据组后操作
    text_ls = p.xpath('.//text()')
    print(''.join(text_ls).replace('\r\n','').replace(' ',''))
python 复制代码
import requests
#from lxml import etree
from bs4 import BeautifulSoup
url="链接"
headers={
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36'
}
res=requests.get(url,headers=headers)
res.encoding='utf-8'
#print(res.text)
#创建BeautifulSoup对象
#BeautifulSoup(html字符串数据,html的解析器)
bs=BeautifulSoup(res.text,'lxml')
lis=bs.select('#browserItemList li')
#print(ls)
count=1
for item in lis:
    title=item.find('a',class_="l").text
    des=item.find('p',class_='info tip').text.replace('\n','').replace('\r','').replace(' ','')
    fade=item.find('small',class_="fade").text
    ren=item.find('span',class_="tip_j").text

    print(count,title,des,fade,ren)
    count+=1

安装模块

复制代码
lxml :解析器              pip install lxml
复制代码
pip install beautifulsoup4
相关推荐
编码者卢布7 分钟前
【Azure Storage Account】Azure Table Storage 跨区批量迁移方案
后端·python·flask
可触的未来,发芽的智生10 分钟前
狂想:为AGI代称造字ta,《第三类智慧存在,神的赐名》
javascript·人工智能·python·神经网络·程序人生
Kira Skyler18 分钟前
eBPF debugfs中的追踪点format实现原理
linux
吴维炜33 分钟前
「Python算法」计费引擎系统SKILL.md
python·算法·agent·skill.md·vb coding
2501_927773071 小时前
uboot挂载
linux·运维·服务器
FansyMeng1 小时前
VSCode配置anaconda
vscode·python
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [drivers][dma]dmapool
linux·笔记·学习
电饭叔2 小时前
Tkinter Button 括号内的核心参数详解
python·学习
ktoking2 小时前
Stock Agent AI 模型的选股器实现 [五]
人工智能·python
地球资源数据云2 小时前
SCI制图——云雨图
python·信息可视化·数据分析