簡體   English   中英

有人可以幫我從名為 Suncalc.org 的網站獲取實時數據嗎

[英]Can someone help me to get live data from a website named Suncalc.org

我使用 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>

這三個點實際上是數字,我需要這些數字。

您好,我測試了您的代碼,似乎網站在瀏覽器請求信息之前不會加載數據。 由於您使用的是請求模塊,因此沒有瀏覽器。

您需要使用像 selenium 模塊這樣的瀏覽器模擬器來獲取該數據。 該模塊將為您打開一個瀏覽器,您可以對其進行編程以導航到該網站,等待所有內容加載完畢並為您獲取信息。

腳步:

1-安裝硒

2-下載 chromedriver 並將其放在某處(可能在您的項目中)

https://chromedriver.chromium.org/downloads

3-Learn selenium(這是一個很棒的自動化網絡導航工具)。 這是一個未經測試的示例,因此您可以得到一個想法(可能立即對您有用,但可能不會)

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.chrome.webdriver 導入 WebDriver

從 selenium.webdriver.common.keys 導入密鑰

導入時間

驅動程序:WebDriver=webdriver.Chrome(executable_path="D:\\download\\chromedriver_win32\\chromedriver.exe")

driver.get(" https://suncalc.org/#/12.05,80.04,17/null/null/324.0/2 ")

時間.sleep(5)

海拔 = driver.find_element_by_id("sunhoehe")

時間.sleep(5)

打印(高度.文本)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM