【问题标题】:Read dynamic webpage html in either Python or R在 Python 或 R 中读取动态网页 html
【发布时间】:2018-08-27 02:35:06
【问题描述】:

我正在尝试自动化抓取网页表格的过程,例如Investing.com Economic Calendar 如果我们只对显示今天日历的默认选项卡感兴趣,这对 R 来说相当简单。这是R代码:

library(rvest)
library(dplyr)

Econ_webpage <- read_html("https://www.investing.com/economic-calendar/")

Indicators  <- Econ_webpage %>% html_nodes("#economicCalendarData") %>% 
html_table(fill = TRUE)  %>% .[[1]] %>% .[-(1:3),-  c(match("Imp.",colnames(.)),ncol(.))]

这会产生下面显示的所需结果。

> head(Indicators)
   Time Cur.                             Event Actual Forecast Previous 
4 19:50  JPY           BoJ Summary of Opinions                          
5 19:50  JPY              Exports (YoY)  (Feb)            1.9%    12.3% 
6 19:50  JPY              Imports (YoY)  (Feb)           17.1%     7.9% 
7 19:50  JPY              Trade Balance  (Feb)           -100B    -944B 
8 20:01  GBP Rightmove House Price Index (MoM)                     0.8% 
9 21:30  CNY         House Prices (YoY)  (Feb)                     5.0%

但是,如果我想在 Tomorrow 选项卡中抓取表格,我需要使用 Selenium 驱动程序。我已经尝试过 RSelenium,但无法让它在我的机器上工作,所以我在 Python 中尝试了 Selenium。我在 Python 中使用以下代码:

import selenium
from selenium import webdriver 

driver.Chrome(executable_path=PATH_TO_CHROMEDRIVER)
driver.get("https://www.investing.com/economic-calendar/")
driver.find_element_by_id("timeFrame_tomorrow").click()
html = driver.page_source

现在我有一个包含所需表数据的 html 字符串,我根本不知道如何有效地解析以生成 R 代码的结果。我可以以某种方式调用 rpy2 包,它允许在 Python 中使用 R 代码,或者其他人知道以与上面相同的形式提取表格的更简单方法?如何解析这个 html 字符串?

【问题讨论】:

标签: python r selenium web-scraping rvest


【解决方案1】:

R 中使用RSelenium,我们可以尝试

library(RSelenium)
library(XML)

rD <- rsDriver()
remDr <- rD[["client"]]
remDr$navigate("https://www.investing.com/economic-calendar/")
option <- remDr$findElement("id", "timeFrame_tomorrow")
option$clickElement()
res <- readHTMLTable((remDr$getPageSource()[[1]]))$economicCalendarData
res <- res[-1,]
head(res)
#   Time Cur. Imp.                       Event Actual Forecast Previous 
#2 02:30  GBP      Investing.com GBP/USD Index                    46.5% 
#3 02:30  USD         Investing.com Gold Index                    65.6% 
#4 02:30  USD      Investing.com S&P 500 Index                    70.7% 
#5 02:30  CAD      Investing.com USD/CAD Index                    41.8% 
#6 02:30  CHF      Investing.com USD/CHF Index                    53.8% 
#7 02:30  AUD      Investing.com AUD/USD Index                    47.9% 


remDr$close()
rD[["server"]]$stop() 

【讨论】:

  • 感谢您的回答,但如前所述,我无法在 R 中启动驱动程序客户端。我收到以下错误 Selenium 消息:未知错误:无法发现打开的页面(驱动程序信息:chromedriver=2.37 .543627 (63642262d9fb93fb4ab52398be4286d844092a5e),platform=Windows NT 6.1.7601 SP1 x86_64) (警告:服务器未提供任何堆栈跟踪信息)错误:摘要:未知错误详细信息:处理命令时发生未知的服务器端错误。更多细节:运行 errorDetails 方法
  • 我已经搜索了好几个小时试图解决这个问题,因为设置 Rselenium 会有很大的帮助,但我得到了越来越多的模糊错误。
  • 我得到了同样的错误。当我在 R 中启动 Selenium 时,Chrome 会打开,但随后会弹出 Chrome Automation Tool crashed。
  • @user3612816 可能是由于某些版本的 chrome。尝试重新安装chrome。顺便说一句,你的 R 版本和包版本是什么?
  • 我使用 Beautifulsoup 和 pandas 在 Python 中解决了这个问题。一旦导航到正确的网站,只需调用 dfs = pd.read_html(driver.page_source)。
猜你喜欢
  • 2012-12-07
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 2020-07-30
  • 2021-04-24
  • 1970-01-01
  • 2020-05-24
相关资源
最近更新 更多