【问题标题】:rvest: getting links from css node error: no applicable method for 'xml_find_all'rvest:从 css 节点获取链接错误:“xml_find_all”没有适用的方法
【发布时间】:2017-09-09 09:37:28
【问题描述】:

我想从页面上的分页确定页数: https://aplikacje.nfz.gov.pl/umowy/Provider/Index?ROK=2017&OW=07&ServiceType=03&Code=&Name=&City=&Nip=&Regon=&Product=&OrthopedicSupply=false

============
Table
============
     Pagination: Link1, Link2, Link3, Link4, LinkNext,Link Last

使用选择器小工具,我发现分页位于“.pagination-container, a”中

我愿意

  1. 将分页中的所有链接转储到向量或data.frame
  2. 获取网址字符串中的最后一个数字
  3. 确定最大页数,指示分页中有多少页,以便稍后在抓取循环中使用它

关注http://francojc.github.io/web-scraping-with-rvest/

我从

开始
library(tidyverse)
library(rvest)

url <- "https://aplikacje.nfz.gov.pl/umowy/Provider/Index?ROK=2017&OW=07&ServiceType=03&Code=&Name=&City=&Nip=&Regon=&Product=&OrthopedicSupply=false"

urls <- url %>% # feed `main.page` to the next step
  html_nodes(".pagination-container, a") %>% # get the CSS nodes
  html_text("href")  

在 html_nodes 上会抛出错误

Error in UseMethod("xml_find_all") : 
  no applicable method for 'xml_find_all' applied to an object of class "character"

我做错了什么?

【问题讨论】:

  • read_html() 在哪里?而且,您可能想要html_attr("href")html_text("href")

标签: r web-scraping rvest


【解决方案1】:

除了“拼写错误”(即错过了对read_html() 的调用)之外,还有一种更简单的方法可以获取总页数。只需在分页器中定位[&gt;&gt;] 链接:

library(rvest)
library(stringi)
library(tidyverse)

url <- "https://aplikacje.nfz.gov.pl/umowy/Provider/Index?ROK=2017&OW=07&ServiceType=03&Code=&Name=&City=&Nip=&Regon=&Product=&OrthopedicSupply=false"

pg <- read_html(url)

html_nodes(pg, "li.PagedList-skipToLast > a") %>% 
  html_attr("href") %>% 
  stri_match_last_regex("page=([[:digit:]]+)") %>% 
  .[,2]
## [1] "13"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 2020-03-05
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多