我正在尝试使用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] 删除。
我来说两句