段落
百度爱奇艺
test.html 中的代码
Title
标题1
标题2
标题3
标题4
test.py 中的代码
from bs4 import BeautifulSoup
with open('./test.html', encoding='utf-8') as f:
html_doc = f.read()
soup = BeautifulSoup(html_doc, 'html.parser')
div_node = soup.find('div', id='content')
print(div_node)
print('='*20)
links = div_node.find_all('a')
for link in links:
print(link.name, link['href'], link.get_text())
img = div_node.find('img')
print(img['src'])
代码运行结果