Beautifulsoup 和 selenium 在我使用 soup.find() 时返回 none 值
Beautifulsoup and selenium returning none value when I use soup.find()
我已经在该站点上搜索了一段时间,但我无法找到解决我的问题的方法。我对网络抓取还很陌生,并尝试使用漂亮的汤和硒从网页中简单地提取一些值。
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)
在最基本的层面上,我所做的只是访问网站内的特定标签。
对于温度,它可以工作,但对于信息和风,我得到 none,我在其他网站上也有同样的问题。我认为问题是我没有得到完整的 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°
None
None
您离目标很近了 - 只需 select 您的元素更具体即可获取您的信息:
...
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
我已经在该站点上搜索了一段时间,但我无法找到解决我的问题的方法。我对网络抓取还很陌生,并尝试使用漂亮的汤和硒从网页中简单地提取一些值。
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)
在最基本的层面上,我所做的只是访问网站内的特定标签。
对于温度,它可以工作,但对于信息和风,我得到 none,我在其他网站上也有同样的问题。我认为问题是我没有得到完整的 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° None None
您离目标很近了 - 只需 select 您的元素更具体即可获取您的信息:
...
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