使用BeautifulSoup模块解析HTML(文件example.html)

原文:

<!-- This is the example.html example file. -->
<html><head><title>The Website Title</title></head>
<body>
<p>Download my <strong>Python</strong> book from <a href='http://inventwithpython.com'>learn Python the easy way!</a>.</p>
<p>By <span id='author'>Al Sweigart</span></p>
</body>
</html>

代码:

import requests,bs4

exampleFile = open('example.html')
exampleSoup = bs4.BeautifulSoup(exampleFile.read(),features='html.parser')
elems = exampleSoup.select('span')

print(elems[0].getText())
print(elems[0].attrs)

输出:

Al Sweigart
{'id': 'author'}

猜你喜欢

转载自www.cnblogs.com/leisurelyRD/p/10758952.html
今日推荐