【问题标题】:How can I scrape all the information from a dropdown menu? [duplicate]如何从下拉菜单中抓取所有信息? [复制]
【发布时间】:2016-04-13 06:31:28
【问题描述】:

我需要从下拉菜单中的所有选项中获取学校名称和状态。

网址是http://www.speechanddebate.org/aspx/rankings.aspx?navid=608&pnavid=2

我试图按照我在此处找到的示例在 R 中编写此代码,但无法正确安装软件包。

我需要一个非常基本的方法,最好是在 R 中,关于如何从下拉菜单中获取学校名称和状态。

【问题讨论】:

  • rvest 非常适合基本的网络抓取,但在这种情况下,它无法为您更改下拉列表,因为这样做不会更改 URL。为此,您需要像 RSelenium 这样的东西,虽然有据可查,但它更强烈一些。
  • 扩大RSelenium的使用范围:stackoverflow.com/questions/26963927/…stackoverflow.com/questions/31616734/…。对于这种情况,我认为这是要走的路。不要忘记检查您要抓取的网站的 ToU!

标签: r web-scraping


【解决方案1】:

如果您只想要条目列表,这里有一个建议。它仅使用基本功能,因为您似乎难以安装软件包(可能是防火墙/代理)

urlink <- "http://www.speechanddebate.org/aspx/rankings.aspx?navid=608&pnavid=2"
alllines <- readLines(urlink)
startidx <- (which(grepl("-- View All Districts --", alllines, fixed=T)) + 1)
endindices <- which(grepl("</select>", alllines, fixed=T))
endidx <- head(endindices[endindices > startidx],1)
alllines[startidx:endidx]
mylist <- unname(na.omit(sapply(alllines[startidx:endidx], 
    function(s) strsplit(strsplit(s, ">")[[1]][2], "<")[[1]][1])))

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多