【问题标题】:how to scrape a hyperlink by R and keep the hyperlink clickable in the output file?如何通过 R 抓取超链接并在输出文件中保持超链接可点击?
【发布时间】:2019-10-02 00:56:43
【问题描述】:

我是一名 R 初学者,正在尝试查看 StackOverflow 中所有前 500/1000 个投票/常见问题。

我需要一个带有两个变量的 data.frame,分别包含问题和该问题的超链接。

网页是here,需要这样的超链接:

        <h3><a href="/questions/5963269/how-to-make-a-great-r-reproducible-example" class="question-hyperlink">How to make a great R reproducible example</a></h3>

输出如下:

         question                                    link
    1    how-to-make-a-great-r-reproducible-example  <questions/5963269/how-to-make-a-great-r-reproducible-example>
    2 ...
    3 ...

抱歉我的困惑问题: 我想拥有可以通过单击链接到网页的超链接。所以我得到了完整的网址

web <- data.frame(sapply(answer$link, function(x) {paste("https://stackoverflow.com",x, sep = "")}))

web <- data.frame(sapply(df[2], function(x) {paste("https://stackoverflow.com",x, sep = "")}))

web[1]
https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

喜欢 [这里] (How to make a great R reproducible example)

我将此文件输出为 txt 或 CSV,但是当我单击它时链接无法链接页面。

你能改进它吗?再次感谢

@DaveT @QHarr

【问题讨论】:

  • 使用适当的 API
  • 您必须在每个链接的开头附加“stackoverflow.com”才能使其可点击。但是 .txt 或 .csv 文件不理解什么是超链接。如果您希望超链接处于活动状态,则需要将信息保存为更现代的文件类型,例如 Word、Excel,甚至可能是 RFT。
  • 我尝试使用 openxlsx 包将它们输出到 excel 中。那没起效。也许我做错了。我认为这里的主要问题是所有链接在您抓取它们时都会转换为字符串。他们失去了超链接的属性。

标签: r web-scraping hyperlink


【解决方案1】:

这是一个使用 rvest 包的直截了当的问题。主要是读取页面,使用 CSS 选择器提取所需的节点,然后提取请求的信息。
这里棘手的部分是隔离仅与问题相关联的链接,而与其他问题无关。在这种情况下,我需要 3-4 级 CSS 标签来完成分离。

请参阅代码中的 cmets 以获取分步说明。

library(rvest)

url<-"https://stackoverflow.com/questions/tagged/r?tab=votes&page=1&pagesize=50"

#read the page
page<-read_html(url)

#get hyperlink nodes
#the 'a' tag under a 'h3' tag under 'div' tag of class 'summary' under a 'div' tag of class 'question-summary'
nodes<-html_nodes(page, "div.question-summary div.summary h3 a")

#Get text
question<-html_text(nodes)
#get link
link<-paste0("https://stackoverflow.com", html_attr(nodes, "href"))

answer<-data.frame(question, link)
head(answer)
                                                         question                                                                             link
1                       How to make a great R reproducible example                    /questions/5963269/how-to-make-a-great-r-reproducible-example
2                    How to sort a dataframe by multiple column(s)                   /questions/1296646/how-to-sort-a-dataframe-by-multiple-columns
3      How to join (merge) data frames (inner, outer, left, right)          /questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right
4 Grouping functions (tapply, by, aggregate) and the *apply family   /questions/3505701/grouping-functions-tapply-by-aggregate-and-the-apply-family
5                                  Drop data frame columns by name                               /questions/4605206/drop-data-frame-columns-by-name
6  Remove rows with all or some NAs (missing values) in data.frame /questions/4862178/remove-rows-with-all-or-some-nas-missing-values-in-data-frame

【讨论】:

  • 谢谢。作为我在这里修改的问题,你知道如何通过 R 输出文本的超链接吗? @DaveT
【解决方案2】:

您应该真正使用 API Stack 提供。但是,您可以使用一级 css 选择器来按类属性收集 a 标签,然后使用 tidyverse 功能将 text 分离为 href;然后也许会生成一个小标题...

library(tidyverse)
library(rvest)

nodes <- read_html('https://stackoverflow.com/questions/tagged/r?tab=votes&page=1&pagesize=50')%>%html_nodes("[class=question-hyperlink]")

df <- map_df(nodes,~{
  questions = .x %>% html_text()
  links =  paste0('https://stackoverflow.com',.x %>% html_attr("href") )
  tibble(questions, links)
})

【讨论】:

  • 谢谢。作为我在这里修改的问题,你知道如何通过 R 输出文本的超链接吗? @QHarr
  • 一个链接可以点击并链接到网页。我尝试将它们输出到excel。那没起效。也许我做错了。
  • 我不知道怎么做。您可以轻松地将链接作为文本写入 csv,但您需要单击每个单元格以使它们成为实际的超链接。
猜你喜欢
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 2012-08-28
  • 2011-11-06
  • 2017-02-02
  • 1970-01-01
  • 2010-10-26
  • 2014-01-21
相关资源
最近更新 更多