python - 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()
解决方案
要在中查找<h2>
Element ,您可以在找到的 上使用另一种方法:<div>
.find()
<div>
div = soup.find('div', class_='col-xs-12')
heading = div.find('h2')
推荐阅读
- android - 如何将数据库连接到 Android 应用程序?
- c++ - 如何找到我在 C++ 代码中犯的错误?
- python - 如何将 3d Nifti(.nii) 图像切片为具有特定平面的 2d 图像?
- python - 如何检查数据帧中的时间增量是否大于一分钟?
- servlets - 当 Web 应用程序在由 Maven 驱动的 Vaadin 14 Web 应用程序中启动时,将我的 Java 代码加载的数据或配置文件放在哪里
- rest - Google Cloud Data Fusion——从 REST API 端点源构建管道
- apache-mina - 远程请求上的 apache mina FTPS 不起作用
- c++ - 定义模板参数的迭代器类型
- spring-boot - 如何使用springdoc在swagger openapi规范3.0的@RequestBody中创建多个模式?
- azure - 逻辑应用订阅事件网格域中的主题