【问题标题】:R download from aspx in https getting website instead of CSVR从https中的aspx下载网站而不是CSV
【发布时间】:2015-11-11 16:33:58
【问题描述】:

警告:新手在这里。我会很感激一些指导。我正在尝试投资学习如何使用 R 实现下载自动化。

我需要什么: 从本网站下载所有县和报告期的页岩气井数据: https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCounty.aspx (注意,进入时可能会询问是否同意,没什么大不了的)

我可以访问列出我要下载的所有 CSV 文件的页面。不幸的是,该站点的地址与上述相同。 (您可以尝试选择一个县和一个报告期,自己看看)

但是,一旦进入该页面,就会列出激活 CSV 下载的链接。对于他们每个人来说都是这样的: https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=15AUGU&COUNTY=ALLEGHENY

我尝试过的:

library(downloader)

download ("https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=15AUGU&COUNTY=ALLEGHENY",
          destfile="Prod_AUG15_Allegheny.csv")

我关注了另一个人在这里所做的事情: Download documents from aspx web page in R

问题: 此命令保存网站而不是 csv 文件。

trying URL 'https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=15AUGU&COUNTY=ALLEGHENY'
Content type 'text/html; charset=utf-8' length 11592 bytes (11 Kb)
opened URL
downloaded 11 Kb

问题: 它与我的页面是 https 而不是 http 有关吗? 关于如何解决它或其他相关帖子的任何指导? (我可以找到一些关于 aspx 下载的帖子,但没有任何帮助)

提前致谢

【问题讨论】:

  • 它使用 SharePoint 并跟踪会话信息和“查看状态”信息(在 hades 中有一个特殊的位置用于 microsoft web ppl)。您需要使用 selenium 并使用“点击”来自动下载数据。
  • 感谢 hrbrmstr!我正在检查此选项,但我更愿意尝试使用 R,因为我正在进行投资。但是,我现在明白了,当它打开 URL 时,我必须将视图状态信息和会话提供给 R。将寻找有关此的信息。欢迎任何其他 cmets!
  • 哦,你仍然可以在 R 中做到这一点cran.rstudio.org/web/packages/RSelenium/vignettes/…
  • 哦,这里是心灵感应。我去找找,谢谢!

标签: asp.net r csv https download


【解决方案1】:

@hrbrmstr 成功了!不是我一开始想要的方式,但使用 RSelenium,我可以单击接受协议的按钮并实际打开下载链接。

这是代码(很简单,但花了我一整天才找到,真可惜):

# Using RSelenium to save file
##Installing the package if needed
install.packages("RSelenium")
##Activating 
library("RSelenium")
checkForServer()
startServer()
#I had to start the server manually!
remDr <- remoteDriver()
remDr
remDr$open()
#open website and accepting conditions
remDr$navigate("https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Welcome/Agreement.aspx")
AgreeButton<-remDr$findElement(using = 'id', value="MainContent_AgreeButton")
AgreeButton$highlightElement()
AgreeButton$clickElement()

remDr$navigate("https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=15AUGU&COUNTY=ALLEGHENY")

但是!!我无法保存 csv 文件 :-(。我知道我需要一个“将链接另存为...”的命令,但我在另一个与 RSelenium 相关的主题中问这个问题。

我发现后会编辑答案!

【讨论】:

    猜你喜欢
    • 2016-08-30
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多