python - 为什么 Beautifulsoup 不从该页面返回所需的项目?
问题描述
我正在按照教程进行操作,但是无论我要求什么,例如 menuitem = page_soup.findAll("h5") 它都没有返回任何内容,但是我知道它们存在,我可以看到它们,但是它没有发现我在做的事情与教程完全一样它一直说什么都没有,我试图从工厂现场拉出并以我的语言找到植物的名称,该名称在页面示例中显示和可见: https ://identify.plantnet.org/observation/weurope/1007256673
我试图从那个页面上得到一个单词,这似乎是不可能的,因为汤一直说,当他们提供帮助时,事情不存在
解决方案
数据是从他们的 API 以 Json 格式动态加载的,所以 BeautifulSoup 看不到它。但是你可以使用requests
模块来加载它:
import json
import requests
url = 'https://identify.plantnet.org/observation/weurope/1007256673'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0'}
api_url = 'https://api.plantnet.org/v1/projects/weurope/observations/{plant_id}?lang=en'
plant_id = url.split('/')[-1]
data = requests.get(api_url.format(plant_id=plant_id), headers=headers).json()
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
# print some data to screen:
print('{} - {}'.format(data['submittedName'], data['species']['commonNames'][0]))
印刷:
Solanum dulcamara L. - Bittersweet
推荐阅读
- wordpress - IBM 文档翻译器使用 WP_Http_Curl 我收到错误消息,例如 Error:415 Unsupported Media Type
- java - 在播放框架中找不到 com.typesafe.play
- docker - 无法通过主机名使用 docker-compose 连接另一个容器中的服务
- spring - 带有restTemplate.delete和Exception的Mockito测试服务方法
- mongodb - 将 mongodb 从 4.0 升级到 4.4 加载现有数据失败
- java - 使用 application.yml 创建数据库模式
- selenium - 无法从带有硒的弹出窗口中找到元素
- javascript - 如何选择一个一次使用 javascript 或 jQuery
- ios - Spotify PKCE 授权流程返回“code_verifier 不正确”
- javascript - 如何将画布保存到特定位置?