原文:
<!-- 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'}