有人能帮我从一个名为Suncalc.org的网站上获取实时数据吗

2024-10-01 09:28:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用beautifulsoup从该网站获取数据。 我的代码:

import bs4
import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.suncalc.org/#/12.98,80.188,10/2020.02.21/15:51/1/3')
soup = BeautifulSoup(r.content,'html.parser')
week = soup.find(id='clickSunrise')

print(week)

结果:

<span class="sunriseX Bold sunrise-time" id="clickSunrise" style="white-space:nowrap;">...</span>

这三个点实际上是数字,我需要这些数字


Tags: 代码fromimportidget网站数字requests
2条回答

您好,我测试了您的代码,似乎在浏览器请求信息之前,网站不会加载数据。由于您使用的是“请求”模块,因此没有浏览器

您需要使用浏览器仿真器(如selenium模块)来获取该数据。 此模块将为您打开一个浏览器,您可以对其进行编程以导航到该网站,直到加载所有内容并为您获取信息

步骤:

1-安装selenium

2-下载chromedriver并将其放在某个地方(可能在您的项目中)

https://chromedriver.chromium.org/downloads

3-Learn selenium(这是一个自动导航web的神奇工具)。这是一个未经测试的示例,只是为了让您能够获得一个想法(可能会立即对您有效,但可能不会)

import time
from selenium import webdriver

driver = webdriver.Chrome('/path/to/chromedriver')  # Change this to your chromedriver path.

driver.get('https://www.suncalc.org/#/12.98,80.188,10/2020.02.21/15:51/1/3');
time.sleep(5) # Let the user actually see something!
clickSunrise = driver.find_element_by_id('clickSunrise')
print(clickSunrise.text)

我希望这有帮助

从selenium导入webdriver

从selenium.webdriver.chrome.webdriver导入webdriver

从selenium.webdriver.common.keys导入密钥

导入时间

驱动程序:WebDriver=WebDriver.Chrome(可执行文件\u path=“D:\download\chromedriver\u win32\chromedriver.exe”)

获取驱动程序(“https://suncalc.org/#/12.05,80.04,17/null/null/324.0/2”)

时间。睡眠(5)

高度=驾驶员。通过id(“sunhoehe”)查找元素

时间。睡眠(5)

打印(高度文本)

相关问题 更多 >