首页 > 解决方案 > BeautifulSoup 返回“无”

问题描述

我接受获取 h2-tag 文本,但它显示“无”。它存在。我试图改变 page.content 'html.parser to lxml' 等等。仍然无法正常工作。

from bs4 import BeautifulSoup
import requests

headers = {"User-agent":"Mozilla/5.0 (X11; Linux x86_64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 
Safari/537.36"}
url = 'http://www.unitedstateszipcodes.org/54115'
page = requests.get(url,headers=headers).text
soup = BeautifulSoup(page, 'html.parser')
div = soup.find('div', class_='col-xs-12').h2
print(div)

之后我尝试更改 xml 中的页面内容elif len(markup) <= 256 and ( TypeError: object of type 'Response' has no len()

标签: pythonhtmlbeautifulsoup

解决方案


要在中查找<h2>Element ,您可以在找到的 上使用另一种方法:<div>.find()<div>

div = soup.find('div', class_='col-xs-12')
heading = div.find('h2')

推荐阅读