我已經在該網站上挖掘了一段時間,但我無法找到解決我的問題的方法。我對網路抓取相當陌生,并試圖使用漂亮的湯和硒從網頁中簡單地提取一些值。
from bs4 import BeautifulSoup
from selenium import webdriver
import requests
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
url = "https://meteofrance.com/previsions-meteo-france/nanterre/92000"
driver = webdriver.Chrome(PATH)
driver.get(url)
time.sleep(3)
page = driver.page_source
driver.quit()
soup = BeautifulSoup(page, 'html.parser')
temp = soup.find('strong', class_="temp")
info = soup.find('p', class_="svg_container")
wind = soup.find('strong', class_="wind-speed")
print(temp.text)
print(info)
print(wind)
在最基本的層面上,我所要做的就是訪問網站中的特定標簽。
對于臨時來說,它可以作業,但對于資訊和風,我沒有得到任何資訊,而且我在其他網站上也遇到了同樣的問題。我認為問題是我沒有獲得完整的 HTML,但我找不到解決方案。我找到的唯一解決方案是使用硒,但我的問題保持不變。
我希望有一個人可以幫助我 !
謝謝Zway
from bs4 import BeautifulSoup
from selenium import webdriver
import requests
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
url = "https://meteofrance.com/previsions-meteo-france/nanterre/92000"
driver = webdriver.Chrome(PATH)
driver.get(url)
time.sleep(3)
page = driver.page_source
driver.quit()
soup = BeautifulSoup(page, 'html.parser')
temp = soup.find('strong', class_="temp")
info = soup.find('p', class_="svg_container")
wind = soup.find('strong', class_="wind-speed")
print(temp.text)
print(info)
print(wind)
這是我執行時得到的
17° 無 無
uj5u.com熱心網友回復:
您已接近目標 - 只需選擇更具體的元素即可獲取您的資訊:
...
temp = soup.find('strong', class_="temp")
info = soup.find('div', class_="svg_container").img['title']
wind = soup.find('p', class_="wind-speed").text.strip()
print(temp.text)
print(info)
print(wind)
...
輸出:
18°
Très nuageux
10
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/451808.html
上一篇:SessionNotCreatedException:訊息:會話未創建:此版本的ChromeDriver僅支持使用Selenium和Python的Chrome版本96
