Python安全笔记(一),python笔记
Python安全笔记(一),python笔记
BeatifulSoup用法
1、BeatifulSoup安装
BeatifulSoup是常用的Python的扩展包,用于对web文件格式化显示、按条件查询等功能。它是bs4包中的一部分,使用前需安装bs4:
pip install bs4
并在python程序中导入:
from bs4 import BeatifulSoup
2、常用用法:
2.1 对下载的网页按格式显示:
r = requests.get("www.baidu.com")
cont = r.content
soup = BeautifulSoup(doc, "html.parser")
print(BeatifulSoup.prettify()) //将html文件按缩进格式显示
2.2 对网页按标签、按属性、按内容检索
例子:
html_doc = '''
<html><head><title>The Dormouse's Story</title></head>
<body>
<p class="title"><b>The Dormouse's Story</b></p>
<asdasd>hello world</asdasd>
<p class="story">Once upon a time,there were three little sisters,and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story"> ...</p>
'''
2.2.1 检索出所有p标签的内容
1)打印出所有名称为“P”的标签,结果可以按列表的方式使用,比如使用index
soup=BeautifulSoup(html_doc, "html.parser")
print(soup.find_all("p"))
2) 打印出所有名称为“P”的标签中的第一个
print(soup.find_all("p")[0]
结果为:
<p class="title"><b>The Dormouse's Story</b></p>
3)分行打印出所有名称为“a”的标签,
for i in soup.find_all(“a”):
print(i)
结果为
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
4)也可以结合正则表达式用,以下返回名称以p开头的标签:
soup.find_all(re.compile("^p")
5)可以根据内容检索,以下打印出包含id=link2的标签:
print(soup.find_all(id="link2"))
结果为:
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
打印出包含"id=link2"的标签中的href元素:
print(soup.find_all(id="link2")[0]["href"])
结果为:
http://example.com/lacie
6) 可以根据标签中含的多个元素联合查询,以下打印出“id=link2”且“class="sister"的标签(注意class要写成class_):
print(soup.find_all(id="link2",class_="sister"))
结果是:
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
评论暂时关闭