我一直在使用Python和Selenium从特定的州健康网页中获取数据,并将该表输出到本地CSV
我在其他几个州使用类似的代码取得了很多成功。但是,我遇到了一种状态,即使用看起来像R的东西来创建动态仪表板,而我无法使用常规方法真正访问这些仪表板
我花了很多时间梳理StackOverflow。我已经检查了是否有一个iframe可以切换,但是,我只是没有看到页面上iframe中我想要的数据
使用Chrome的“检查”功能,我可以很容易地找到表格信息。但是,从原始URL开始,我需要的数据不在该页面上,并且我找不到表的源URL。我甚至用Fiddler来看看某处是否有电话
所以,我不知道该怎么办。我可以看到数据——但是,我不知道在哪里告诉Selenium和BS4在哪里访问它
页面在这里:https://coronavirus.utah.gov/case-counts/
加载页面需要一些时间。我有其他州也有这个问题,Selenium可以解决这个问题
如有任何帮助或建议,将不胜感激
这是我一直在使用的代码。它在这里不起作用,但是,它的结构与其他州的结构非常相似
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
st = 'ut'
url = 'https://coronavirus.utah.gov/case-counts/'
timeout = 20
# Spawn the webpage using Selenium
driver = webdriver.Chrome(r'D:\Work\Python\utilities\chromedriver\chromedriver.exe')
driver.minimize_window()
driver.get(url)
# Let page load . . . it takes a while
wait = WebDriverWait(driver, timeout).until(EC.visibility_of_element_located()((By.ID, "total-number-of-lab-confirmed-covid-19-cases-living-in-utah")))
# Now, scrape table
html = driver.find_element_by_id("total-number-of-lab-confirmed-covid-19-cases-living-in-utah")
soup = BeautifulSoup(html, 'lxml')
table = soup.find_all('table', id='#DataTables_Table_0')
df = pd.read_html(str(table))
exec(st + "_counts = df[0]")
tmp_str = f"{st}_counts.to_csv(r'D:\Work\Python\projects\Covid_WebScraping\output\{st}_covid_cnts_' + str(datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) + '.csv'"
file_path = tmp_str + ", index=False)"
exec(file_path)
# Close the chrome web driver
driver.close()
我找到了另一种方法来获取我需要的信息
感谢朱利安·斯坦利让我了解卡塔隆录音机产品。这让我看到了iframe是什么,桌子在哪里
使用CSS或XPATH查找元素的旧方法会由于线程锁定而导致Pickle错误。我不知道该怎么处理。但是,这导致整个项目停滞不前
但是,我能够通过属性获取表的text/HTML。之后,我像往常一样用BS4阅读
相关问题 更多 >
编程相关推荐