Python关于BeautifulSoup的用法

发布网友 发布时间:2022-03-03 21:17

我来回答

2个回答

热心网友 时间:2022-03-03 22:47

创建一个字符串,例子如下:

Python

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<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>

创建 beautifulsoup 对象

Python
soup = BeautifulSoup(html)

另外,还可以用本地 HTML 文件来创建对象,例如
soup = BeautifulSoup(open('index.html'))

上面这句代码便是将本地 index.html 文件打开,用它来创建 soup 对象
下面来打印一下 soup 对象的内容,格式化输出
print soup.prettify()

<html>
<head>
<title>
The Dormouse's story
</title>

热心网友 时间:2022-03-04 00:05

可以的
soup = BeautifulSoup(获取的数据, fromEncoding='gbk')
items = soup.findAll(attrs = {'class':'small'})

for item in items:
print item
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com