栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

BeautifulSoup实例测试

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BeautifulSoup实例测试

coding:utf-8

from bs4 import BeautifulSoup
import re

html_doc = """
The Dormouse's story

The Dormouse's story

once upon a time there were three little sisters; and their names were
http://example.com/elsie" class="sister" id="link1">Elsie,
http://example.com/lacie" class="sister" id="link2">Lacie and
http://example.com/tillie" class="sister" id="link3">Tillie;
and they lived at the bottom of a well.

...


"""

soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')

print('获取所有的链接')
links = soup.find_all('a')
for link in links:
print(link.name, link['href'], link.get_text())

print('获取lacie的链接')
link_node = soup.find('a', href='http://example.com/lacie')
print (link_node.name, link_node['href'], link_node.get_text())

print('正则匹配ill的链接')
href_link = soup.find('a', href=re.compile(r'ill'))
print (href_link.name, href_link['href'], href_link.get_text())

print('获取p标签的段落内容')
p_node = soup.find('p', 'title')
print (p_node.name, p_node.get_text())

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/224309.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号