使用 bs4 可以很轻松获得网页文字内容,但是如果想获得网页的标签信息应该怎么做?
假如网页源码是
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<p class="title"><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>
</body>
</html>
获得网页标签结构
<html>
<head>
<title> </title>
</head>
<body>
<p class="title"><b> </b></p>
<p class="story">
<a href="http://example.com/elsie" class="sister" id="link1"> </a>
<a href="http://example.com/lacie" class="sister" id="link2"> </a>
<a href="http://example.com/tillie" class="sister" id="link3"> </a> </p>
<p class="story"> </p>
</body>
</html>
1
Droi 2017-08-18 10:37:58 +08:00
你是不是想要这些内容。
我是熬两次汤,熬出来的。不知道其他人有没有其他好用的方法。 官方教程的快速开始有这个 get()方法 from bs4 import BeautifulSoup >>> html = """<html> ..... </html>""" >>> soup = BeautifulSoup(html, "html.parser") >>> ssoup = BeautifulSoup(str(soup.find_all(attrs={'class':'story'})), "html.parser") >>> ssoup.find_all('a')[0].get('href') 'http://example.com/elsie' >>> |