选择器提取
选择器提取
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("page.html", encoding="utf-8"), "lxml")
for p in soup.select("article p"):
print(p.get_text(" ", strip=True))
for a in soup.select("nav a[href]"):
print(a["href"], a.get_text(strip=True))选择器依赖页面结构,站点改版会失效;批量抓取前先确认目标站点的抓取条款。