Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用BeautifulSoup和Selenium解析网站_Python_Selenium_Web Scraping_Beautifulsoup_Html Parsing - Fatal编程技术网

Python 使用BeautifulSoup和Selenium解析网站

Python 使用BeautifulSoup和Selenium解析网站,python,selenium,web-scraping,beautifulsoup,html-parsing,Python,Selenium,Web Scraping,Beautifulsoup,Html Parsing,试图通过从以下位置刮取平均温度与实际温度进行比较: 我可以成功地收集网页的源代码,但我在解析时遇到了困难,无法在“历史记录”选项卡下只给出高温、低温、降雨和平均值,但如果没有得到唯一的结果“无”,我似乎无法处理正确的类/id 这就是我到目前为止所做的,最后一行只是尝试获得高温度: from lxml import html from bs4 import BeautifulSoup from selenium import webdriver url = "https://usclimated

试图通过从以下位置刮取平均温度与实际温度进行比较:

我可以成功地收集网页的源代码,但我在解析时遇到了困难,无法在“历史记录”选项卡下只给出高温、低温、降雨和平均值,但如果没有得到唯一的结果“无”,我似乎无法处理正确的类/id

这就是我到目前为止所做的,最后一行只是尝试获得高温度:

from lxml import html
from bs4 import BeautifulSoup
from selenium import webdriver

url = "https://usclimatedata.com/climate/binghamton/new-york/unitedstates/usny0124"
browser = webdriver.Chrome()
browser.get(url)
soup = BeautifulSoup(browser.page_source, "lxml")
data = soup.find("table", {'class': "align_right_climate_table_data_td_temperature_red"})

首先,这是两个不同的类——
align\u right
temperature\u red
——出于某种原因,您加入了它们并添加了
table\u data\u td
。并且,具有这两个类的元素是
td
元素,而不是
table

在任何情况下,要获取气候表,您应该查找具有
id=“climate\u table”
div
元素:

另一个需要注意的重要事项是,这里可能存在“计时”问题-当您获得
驱动程序.page_source
值时,气候信息可能不在那里。通常在导航到页面后添加一个:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup


url = "https://usclimatedata.com/climate/binghamton/new-york/unitedstates/usny0124"
browser = webdriver.Chrome()

try:
    browser.get(url)

    # wait for the climate data to be loaded
    WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "climate_table")))

    soup = BeautifulSoup(browser.page_source, "lxml")
    climate_table = soup.find(id="climate_table")

    print(climate_table.prettify())
finally:
    browser.quit()
请注意,添加了
try/finally
,可以在出现错误时安全地关闭浏览器,这也有助于避免“挂起”浏览器窗口


而且,看看能神奇地将你的气候信息表读入自动模式的模式。

首先,这是两个不同的类别-
align\u right
temperature\u red
——出于某种原因,你加入了它们并添加了
table\u data\u td
。并且,具有这两个类的元素是
td
元素,而不是
table

在任何情况下,要获取气候表,您应该查找具有
id=“climate\u table”
div
元素:

另一个需要注意的重要事项是,这里可能存在“计时”问题-当您获得
驱动程序.page_source
值时,气候信息可能不在那里。通常在导航到页面后添加一个:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup


url = "https://usclimatedata.com/climate/binghamton/new-york/unitedstates/usny0124"
browser = webdriver.Chrome()

try:
    browser.get(url)

    # wait for the climate data to be loaded
    WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "climate_table")))

    soup = BeautifulSoup(browser.page_source, "lxml")
    climate_table = soup.find(id="climate_table")

    print(climate_table.prettify())
finally:
    browser.quit()
请注意,添加了
try/finally
,可以在出现错误时安全地关闭浏览器,这也有助于避免“挂起”浏览器窗口


而且,看看能将你的气候信息表神奇地自动读入的数据库。

这可能是一个更适合@kuanb nope的问题,在codereview上它会很快关闭-他们只查看工作代码。啊,谢谢@alecxe。我的错。这可能是一个更适合@kuanb nope的问题,在codereview上它会很快关闭-他们只审查工作代码。啊,谢谢@alecxe。我的错。