【问题标题】:Not getting expected output in we scraping R在我们抓取 R 时没有得到预期的输出
【发布时间】:2019-05-30 14:32:41
【问题描述】:

我写了一个小程序。 我在哪里 抓取 Google 搜索网站,我想要 Google 搜索网页上的所有 URL。但是我在 O/P 中得到了字符(0)。请帮帮我。

代码 -

library("rvest")
r_h  = read_html("https://www.google.com/search?q=google&oq=google&aqs=chrome.0.69i59j0l2j69i60l2j69i65.1101j0j7&sourceid=chrome&ie=UTF-8")
d  =  r_h %>% html_nodes(".iUh30") %>% html_text() %>% as.character()

【问题讨论】:

    标签: html r web-scraping rvest


    【解决方案1】:

    返回的 html 中不存在该类。您需要不同的选择器策略,然后提取 href

    library(rvest)
    library(stringr)
    r_h  = read_html("https://www.google.com/search?q=google&oq=google&aqs=chrome.0.69i59j0l2j69i60l2j69i65.1101j0j7&sourceid=chrome&ie=UTF-8")
    d  =  r_h %>% html_nodes(".jfp3ef > a") %>% html_attr(., "href")
    
    for(i in d){
      res <- str_match_all(i,'(http.*?)&')
      print(res[[1]][,2])
    }
    

    【讨论】:

    • 您好,先生/maam。你是怎么得到 jfp3ef > a 的??
    • 我检查了使用 jsoup 返回的实际 html 并编写了一个选择器来匹配您的选择器从浏览器获取的内容
    • 这是你所期望的答案吗?
    • 不.. 我得到一个不起作用的 URL。上课是错的,我想是的。
    • 请尝试以上方法
    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    相关资源
    最近更新 更多