首页 > 解决方案 > 为什么 Beautifulsoup 不从该页面返回所需的项目?

问题描述

我正在按照教程进行操作,但是无论我要求什么,例如 menuitem = page_soup.findAll("h5") 它都没有返回任何内容,但是我知道它们存在,我可以看到它们,但是它没有发现我在做的事情与教程完全一样它一直说什么都没有,我试图从工厂现场拉出并以我的语言找到植物的名称,该名称在页面示例中显示和可见: https ://identify.plantnet.org/observation/weurope/1007256673

我试图从那个页面上得到一个单词,这似乎是不可能的,因为汤一直说,当他们提供帮助时,事情不存在

标签: pythonweb-scrapingbeautifulsoup

解决方案


数据是从他们的 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

推荐阅读