python:无法使用BeautifulSoup从html获取特定数据

亚历克斯

我正在尝试使用beautifulsoup和urllib从特定网页中获取给定的百分比:https ://app.safespace.io/api/display/live-occupancy/86fb9e11?view=percent 我对这样的东西很陌生。这是我的意大利面代码:

import urllib.request

contentSource = urllib.request.urlopen('https://app.safespace.io/api/display/live-occupancy/86fb9e11?view=percent')
read_content = contentSource.read()

from bs4 import BeautifulSoup
soup = BeautifulSoup(read_content, 'html.parser')

try1 = soup.find("span", {"id": "occupancyPct"})

print(try1)

在原始网页上,当“检查元素”百分比时,该百分比实际上显示在html中,如突出显示

但是,我的代码的打印输出是 <span class="text-xl" id="occupancyPct" style="margin-bottom: auto;"></span>

请注意,与实际页面的html不同,我的代码的输出如何不显示输出中的百分比。我究竟做错了什么?

我也将接受“您因为X愚蠢而应该使用Y”,或其他形式的变化。

孟德尔

该页面是动态加载的,因此requests不支持该页面我们可以用Selenium作为刮刮页面的替代方法。

使用进行安装pip install selenium

此处下载正确的ChromeDriver

from time import sleep
from selenium import webdriver
from bs4 import BeautifulSoup

URL = "https://app.safespace.io/api/display/live-occupancy/86fb9e11?view=percent"
driver = webdriver.Chrome(r"c:\path\to\chromedriver.exe")
driver.get(URL)
# Wait for page to fully render
sleep(5)

soup = BeautifulSoup(driver.page_source, "html.parser")
print(soup.find("span", {"id": "occupancyPct"}).text)

driver.quit()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Python Beautifulsoup从复杂的html标签获取数据

使用awk从ipconfig获取特定数据

使用python从xml提取特定数据

Python Requests-HTML-无法找到特定数据

Python Requests-HTML-无法从表中提取特定数据

使用BeautifulSoup从html表中获取数据

如何从html网页获取特定数据

无法使用BeautifulSoup获取某些数据

如何使用Javascript从HTML页面的<SCRIPT>标记中获取特定数据

如何使用beautifulsoup从html标记的特定类中获取数据?

如何从JAVA中的html文件获取特定数据

无法使用 BeautifulSoup 从 html 获取日期

使用 BeautifulSoup 从 html 中抓取特定数据

如何在python中使用queryset获取特定数量的数据

VBA HTML 抓取。使用动态网页,但无法指明所需的特定数据点

使用python从网站获取特定数据

如何使用特定按钮获取特定数据

如何使用beautifulsoup从数据框中的URL列表中获取特定html div的内容?

使用python从html解析中提取特定数据

如何使用 BeautifulSoup 抓取特定数据

使用 python 从 JSON 中获取特定数据

使用 Python 从网站获取特定数据

如何使用 Selenium Python 从 HTML 中收集特定数据

使用 PHP 从 MYSQL 获取特定数据

我无法使用beautifulsoup python获取HTML标签的值

使用python获取beautifulsoup的特定行

使用 nodejs 从数据库中获取特定数据

Python BeautifulSoup 无法从具有特定类的 div 获取数据

使用 BeautifulSoup 从 html 获取特定文本