我试图从一个简单的网站上获取所有的标题。我的尝试:
from bs4 import BeautifulSoup, SoupStrainer
import requests
url = "http://nypost.com/business"
page = requests.get(url)
data = page.text
soup = BeautifulSoup(data)
soup.find_all('h')
soup.find_all('h')
返回[]
,但如果我执行soup.h1
或soup.h2
之类的操作,它将返回相应的数据。我只是打错了电话吗?
你需要做
soup.find_all('h1')
你可以这样做:
按正则表达式筛选:
此正则表达式查找以
h
开头、在h
后面有一个数字、然后在该数字后面结束的所有标记。如果您不想使用regex,那么您可能需要执行以下操作:
结果:
相关问题 更多 >
编程相关推荐