【问题标题】:I am learning web scraping in R , facing the error after running the code : Error我正在学习 R 中的网络抓取,运行代码后遇到错误:错误
【发布时间】:2022-01-11 15:04:29
【问题描述】:

我正在学习 R 中的网页抓取,尝试在以下代码的帮助下抓取有关数学堆栈交换的问题页面:

以下代码的目的是收集问题的所有链接,然后抓取问题的可接受解决方案。但是,当我运行代码时,最后出现了错误:

open.connection(x, "rb") 中的错误:HTTP 错误 404。

library(rvest)

link <- "https://math.stackexchange.com/questions/tagged/integration"
url <- read_html(link) 
page <- url %>%   html_nodes(".question-hyperlink")  %>%  html_text()

questions_link <- url %>%   html_nodes(".question-hyperlink") %>% 
  html_attr("href") %>% paste("https://math.stackexchange.com", .,sep = "")


get_answer = function(answer_link){
    answer_page <- read_html(answer_link)
  solution = answer_page %>% html_nodes(".accepted-answer") %>% html_text() 
  return(solution)
}

solution_accepted <- sapply(questions_link, FUN = get_answer) 

你能帮忙解决一下吗,会很有帮助的。谢谢..

【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    问题是在questions_link 中提取的其他链接不是该论坛的问题。它正在从右侧的“网络热点问题”中提取链接。

    进一步过滤链接以仅获取来自论坛的问题。

    library(rvest)
    
    questions_link <- url %>%   
      html_nodes("h3 a.question-hyperlink") %>%
      html_attr("href") %>% 
      paste0("https://math.stackexchange.com", .)
    

    【讨论】:

    • 感谢您的帮助,能否请您也看看这个:link math.stackexchange.com/questions/tagged/integration" url % html_nodes(". question-hyperlink") %>% html_text() 在上面的代码中制作 : page 和 solution_accepted 的数据框,显示一些错误,再次感谢您的帮助..
    猜你喜欢
    • 1970-01-01
    • 2016-08-18
    • 2015-06-16
    • 2023-02-19
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    相关资源
    最近更新 更多