【问题标题】:Scrape a library of literature with rvest用 rvest 刮掉一个文学图书馆
【发布时间】:2022-01-15 15:24:34
【问题描述】:

我正在学习rvest

我打算抓取我的搜索结果。这是网页,

https://pubmed.ncbi.nlm.nih.gov/?term=eliminat+matrix+effect+HPLC-ms%2Fms&filter=years.2013-2022&size=200

我查了html_nodes()。没有我在网页上看到的内容。

我能做什么?

这是“身体”。

webpage %>% html_node('body')
{html_node}
<body>
 [1] <noscript>\n  <div class="no-script-banner" id="no-script-banner">\n    <div class="warning-message">\n      <div class="warn ...
 [2] <div class="no-session-banner" id="no-session-banner" hidden>\n  <div class="warning-message">\n    <div class="warning-messa ...
 [3] <a class="usa-skipnav" href="#search-results">\n    Skip to main page content\n  </a>
 [4] <div role="complementary" id="ncov-alert-from-server" class="ncbi-alerts nCoV_shutdown converted" style="display: block;" dat ...
 [5] <div class="usa-overlay"></div>
 [6] <header class="ncbi-header" role="banner" data-section="Header"><div class="usa-grid">\n\t\t<div class="usa-width-one-whole"> ...
 [7] <div role="navigation" aria-label="access keys">\n<a id="nws_header_accesskey_0" href="https://www.ncbi.nlm.nih.gov/guide/bro ...
 [8] <section data-section="Alerts"><div class="ncbi-alerts-placeholder"></div>\n</section>
 [9] <a id="maincontent" aria-label="Main page content below" role="navigation"></a>
[10] <main class="search-page" id="search-page"><h1 class="usa-sr-only">Search Page</h1>\n    \n    \n\n\n\n<input type="hidden" n ...
[11] <div id="ncbi-footer">\n      <div class="literature-footer" role="complementary" title="Links to NCBI Literature Resources"> ...
[12] <script src="https://cdn.ncbi.nlm.nih.gov/pubmed/0399d7a0-471a-4f7d-84af-66091af9d657/CACHE/js/output.293fbf76aa18.js"></script>
[13] <script src="https://cdn.ncbi.nlm.nih.gov/pubmed/0399d7a0-471a-4f7d-84af-66091af9d657/CACHE/js/output.29588445dbd9.js"></script>
[14] <script>\n    ncbi.awesome.basePage.init({\n      userInfo: {\n        isLoggedIn: false,\n        username: "",\n        log ...
[15] <script type="text/javascript">\n    jQuery.getScript("https://www.ncbi.nlm.nih.gov/core/alerts/alerts.js", function () {\n   ...
[16] <script defer type="text/javascript" src="https://cdn.ncbi.nlm.nih.gov/core/pinger/pinger.js"> </script>
[17] <svg class="timeline-filter-gradient" xmlns="http://www.w3.org/2000/svg"><defs><lineargradient id="timeline-filter-selected-g ...
[18] <script src="https://cdn.ncbi.nlm.nih.gov/pubmed/0399d7a0-471a-4f7d-84af-66091af9d657/CACHE/js/output.714a700656e1.js"></script>
[19] <script>\n    ncbi.awesome.searchPage.init({\n      searchQuery: "eliminat matrix effect HPLC\\u002Dms/ms",\n      searchCons ...
Not 

【问题讨论】:

  • 您是否尝试返回包含所有这些字词的结果(即所有搜索字词都应出现在结果中)? AND/OR 与您的搜索词在哪里匹配?另外,您是否拼错了部分查询?提供的结果是否与正确的搜索字词相符?

标签: r rvest


【解决方案1】:

我们可以通过

得到搜索结果的标题
library(rvest)
library(dplyr)
library(stringr)

url %>% read_html() %>% html_nodes('.docsum-title') %>% html_text() %>% str_remove_all('\\n')

  [1] "                HPLC-MS/MS analysis of peramivir in rat plasma: Elimination of matrix effect using the phospholipid-removal solid-phase extraction method.              "                                                                                                                                             
  [2] "                Development of matrix effect-free MISPE-UHPLC-MS/MS method for determination of lovastatin in Pu-erh tea, oyster mushroom, and red yeast rice.

以及文章的链接

df = url %>% read_html() %>% html_nodes('.docsum-title') %>% html_attr('href') 

paste0('https://pubmed.ncbi.nlm.nih.gov', df)

  [1] "https://pubmed.ncbi.nlm.nih.gov/28976569/" "https://pubmed.ncbi.nlm.nih.gov/28410522/" "https://pubmed.ncbi.nlm.nih.gov/27491846/"
  [4] "https://pubmed.ncbi.nlm.nih.gov/31532096/" "https://pubmed.ncbi.nlm.nih.gov/31288535/" "https://pubmed.ncbi.nlm.nih.gov/29433096/"

你可以看到节点.docsum-title并在它旁边链接。

【讨论】:

  • 谢谢@Nad。识别“.docsum-title”是存储标题的节点的过程是什么?如果我在每篇论文下都需要摘要,我在哪里看?
  • 是的,在节点.docsum-title 下存储了标题。如果我们需要摘要,那么我们将遍历每个链接并在节点.abstract-content selected 下存储摘要。
  • 谢谢你,纳德。您能否告诉我如何知道标题存储在节点 .docsum-title 下?
  • 修改了答案以包含节点的图像。
  • 谢谢 Nad,我想办法得到你分享的图像。我很高兴学习这样的网络抓取工具来节省我在文献搜索上的时间。
【解决方案2】:

我会考虑您的搜索字词是否拼写正确,以及您是否希望在每个字词之间进行 AND 或 OR 以适当地设置您的请求。根据确定这些,您可能会决定使用提供的公共 API 来应用您的查询,提取 pubmed id,然后请求相关的文档。

API 指导:https://www.ncbi.nlm.nih.gov/home/develop/api/

library(jsonlite)
library(rvest)
library(tidyverse)

get_data <- function(link) {
  page <- read_html(link)
  data.frame(
    link = link,
    id = page %>% html_element('[title="PubMed ID"]') %>% html_text(trim = T),
    title = page %>% html_element(".heading-title") %>% html_text(trim = T),
    authors = page %>% html_elements(".full-name") %>% html_text(trim = T) %>% paste(., collapse = ', '),
    abstract = page %>% html_element("#enc-abstract") %>% html_text2()
  )
}

r <- jsonlite::read_json("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmax=200&retmode=json&term=eliminate+AND+matrix+AND+effect+AND+hplc+ms/ms&mindate=2013&maxdate=2022")
ids <- r$esearchresult$idlist

if(length(ids)>0){
  
  links <- sprintf("https://pubmed.ncbi.nlm.nih.gov/%s", ids)
  results <- map_dfr(links, get_data)
  
}

【讨论】:

  • 感谢 QHarr 分享代码。我得到了六篇论文。我想知道为什么我拿不到 101 篇论文?
  • 因为我用 AND 表示只搜索那些包含所有搜索词的结果,结果为六个。用 OR 替换会产生更大的结果数字。我怀疑您的原始请求中有一些 OR。
  • 查看有关指定搜索位置的 API 文档,因为您可以搜索特定/多个数据库以及不同的出版物类型。
  • 我从一门我非常熟悉的语言开始,并且逐一解决了我能找到的所有 StackOverflow 问题。我补充了 YouTube 视频和阅读文章/博客。我订阅了几门课程,但发现它们通常教授糟糕/糟糕/过时的 IMO 方法。通过阅读和尝试各种语言的问题,我拓宽了知识面。这些方法没有太大变化,只有语言和语法/语言习语的限制,因为它往往是关于了解网页是如何更新/结构化的
  • 对于 RI 学习与包相关的文档,依靠其他语言的现有知识,从其他用户在 R + &lt;insert web-scraping related tag here&gt; 中给出的答案中学习你需要扎实的基本 html/JavaScript 知识和一些关于现代网络框架。我的个人资料中有一些链接。
猜你喜欢
  • 1970-01-01
  • 2016-03-08
  • 2020-04-14
  • 2019-11-18
  • 2016-05-29
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多