【发布时间】:2020-09-19 10:05:11
【问题描述】:
所以我尝试使用 selenium 抓取 webs table,尝试使用 xpath 提取表:
之前我试图寻找表格类但是没有找到表格,所以我决定寻找 div 元素。
xpath="//div[@class='table-scroller ScrollableTable__table-scroller QuoteHistoryTable__table__scroller QuoteHistoryTable__QuoteHistoryTable__table__scroller']"
WebDriverWait(driver, 10).until(
expected_conditions.visibility_of_element_located((By.XPATH, xpath)))
source = driver.page_source
driver.quit()
soup = BeautifulSoup(source, "html5lib")
table = soup.find('div', {'class': 'table-scroller ScrollableTable__table-scroller QuoteHistoryTable__table__scroller QuoteHistoryTable__QuoteHistoryTable__table__scroller'})
df = pd.read_html(str(table), flavor='html5lib', header=0, thousands='.', decimal=',')
print(df[0])
我遇到的问题是我只打印标题和第一行充满nans的值:
为什么我没有得到表格的值?是什么让抓取这些内容变得如此困难?
编辑: @DebanjanB 能够提供一个很好的答案,但是我无法复制输出,这背后的原因是什么?
【问题讨论】:
-
BeautifulSoup(html_source, "html5lib") 任何名为 html_source 的东西?
-
stackoverflow.com/questions/63960297/… 你也有类似的问题。只需切换到 CSS 选择器。
-
@arundeepchohan 谢谢你的想法,但将 XPATH 更改为 CSS,问题仍然存在,我用同一行 nans 生成 df,我决定发布一个不同的问题,因为我认为在这种情况下,问题有所不同,因为我访问了框架而不是其中的值。
标签: python selenium selenium-webdriver xpath webdriverwait