【问题标题】:Regular expressions filter all on tag article正则表达式过滤所有标签文章
【发布时间】:2020-03-29 00:24:48
【问题描述】:

我正在尝试从以下发布的文章中获取所有内容: https://genomebiology.biomedcentral.com/articles/10.1186/s13059-020-01975-8 我发现信息在标签里

<article><div...><..> information.... <></article>

我正在尝试这样的事情:

art_sections<-regexpr("<article (.*)?>(.[0-9]*)</article>",thepage)

但我无法检索信息..

请问你知道我该如何解决它。

【问题讨论】:

    标签: r regex web-scraping


    【解决方案1】:

    这不是一个正则表达式问题,而是关于使用 R 使用库进行网络抓取的问题,例如rvest.

    这里是一些示例代码,下面是一些帮助您入门的链接:

    #Loading the rvest package
    library('rvest')
    #Specifying the url for desired website to be scraped
    url <- 'https://genomebiology.biomedcentral.com/articles/10.1186/s13059-020-01975-8'
    #Reading the HTML code from the website
    webpage <- read_html(url)
    article_html <- html_nodes(webpage,'article')
    #Converting the ranking data to text
    html_text(article_html)
    

    最后,要清理您的文本,请查看 stringr

    library(stringr)
    str_replace_all(x, "[\r\n]" , "")
    

    【讨论】:

    • 谢谢,效果很好,但是一开始有很多乱七八糟的字和字符,像这样:\n \n\n 方法\n \n \n \n 打开访问\n \n \n \n \n\n \n 576 tric\n \n .....它不是来自
    • 在标签文章中有很多部分我该怎么做才能只获取这些信息,我需要所有标签部分的信息
    • 识别那些标签/css选择器,用html_nodes提取它们,根据需要清理它们(你原来的问题没有说明什么,老实说,你的问题太宽泛了)。如果您在旅途中遇到特定问题的障碍,只需按照上面给出的指示开始一个新问题。祝你好运。
    • 谢谢你,它工作得很好,但我有很多空格和“\n”你知道我怎样才能删除它们吗?
    • 如何使用 gsub 删除类似以下内容:\n 之后的空格 text \n text \n 空格 text text tex \n (文章,这是我想要的正确)
    【解决方案2】:

    尝试使用rvest 包提取文章中的所有文本(仅文本)。但是,所有 HTML 标记(包括链接、图像等)都会被删除。

    # install.packages("rvest")
    
    library(rvest)
    url <- "https://genomebiology.biomedcentral.com/articles/10.1186/s13059-020-01975-8"
    article <- url %>% 
      read_html %>%
      html_node(css = 'article') %>%
      html_text
    
    article
    
    # Method\n    \n        \n            \n                Open Access\n            \n        \n    \n    \n\n                            Published: 19 March 2020\n                        VALOR2: characterization of large-scale structural variants using linked-reads\n                        Fatih Karaoğlanoğlu1 na1, Camir Ricketts2,4, Ezgi Ebren1, Marzieh Eslami Rasekh3, Iman Hajirasouliha4,5 & Can Alkan1,6 \n                            \n    Genome Biology\n\n                            volume 21, Article number: 72 (2020)\n            Cite this article\n                        \n                        \n    \n        \n            \n                        576 Accesses\n                    \n                \n                \n                    \n                        1 Citations\n                    \n                \n                \n                    \n                        \n                            12 Altmetric\n                        \n                    \n                \n                \n                    Metrics details\n                \n            \n    \n\n                        \n                        \n                        \n                    \n\n                    AbstractMost existing methods for structural variant detection focus on discovery and genotyping of deletions, insertions, and mobile elements. Detection of balanced structural variants with no gain or loss of genomic segments, for example, inversions and translocations, is a particularly challenging task. Furthermore, there are very few algorithms to predict the insertion locus of large interspersed segmental duplications and characterize translocations. Here, we propose novel algorithms to characterize large interspersed segmental duplications, inversions, deletions, and translocations using linked-read sequencing data. We redesign our earlier algorithm, VALOR, and implement our new algorithms in a new software package, called VALOR2.BackgroundAlterations of DNA content and organization larger than 50 bp, commonly referred to as genomic structural variations (SVs) [1], are among the major drivers of evolution [2, 3] and diseases of genomic origin [4]. Despite decades of research, they remain difficult to accurately characterize contributing to our lack of full understanding of the etiology of complex diseases, termed missing heritability [5].High-throughput sequencing
    

    Regex解决方案提取&lt;article&gt;标签之间的所有内容(包括文本和其他HTML标签)

    html <- paste(readLines(url), collapse = " ")
    article <- sub(".*(<article.*?>.*</article>).*", "\\1", html)
    
    article
    
    # <article itemscope itemtype=\"http://schema.org/ScholarlyArticle\" lang=\"en\">                     <div class=\"c-article-header\">                                                   <ul class=\"c-article-identifiers\" data-test=\"article-identifier\">                                  <li class=\"c-article-identifiers__item\" data-test=\"article-category\">Method</li>                           <li class=\"c-article-identifiers__item\">                 <span class=\"c-article-identifiers__open\" data-test=\"open-access\">Open Access</span>             </li>                                                 <li class=\"c-article-identifiers__item\"><a href=\"#article-info\" data-track=\"click\" data-track-action=\"publication date\" data-track-category=\"article body\" data-track-label=\"link\">Published: <time datetime=\"2020-03-19\" itemprop=\"datePublished\">19 March 2020</time></a></li>                         </ul>                          <h1 class=\"c-article-title u-h1\" data-test=\"article-title\" data-article-title=\"\" itemprop=\"name headline\">VALOR2: characterization of large-scale structural variants using linked-reads</h1>                         <ul class=\"c-author-list js-list-authors js-etal-collapsed\" data-etal=\"25\" data-etal-small=\"3\" data-test=\"authors-list\"><li class=\"c-author-list__item\" itemprop=\"author\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\"><a data-test=\"author-name\" data-track=\"click\" data-track-action=\"open author\" data-track-category=\"article body\" data-track-label=\"link\" href=\"#auth-1\">Fatih KaraoÄŸlanoÄŸlu</a></span><sup class=\"u-js-hide\"><a href=\"#Aff1\">1</a><span itemprop=\"affiliation\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Organization\" class=\"u-visually-hidden\"><meta itemprop=\"name\" content=\"Bilkent University\" /><meta itemprop=\"address\" content=\"grid.18376.3b, 0000 0001 0723 2427, Department of Computer Engineering, Bilkent University, Ankara, 06800, Turkey\" /></span></sup><sup class=\"u-js-hide\">Â <a href=\"#na1\">na1</a></sup>, </li><li class=\"c-author-list__item\" itemprop=\"author\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\"><a data-test=\"author-name\" data-track=\"click\" data-track-action=\"open author\" data-track-category=\"article body\" data-track-label=\"link\" href=\"#auth-2\">Camir Ricketts</a></span><sup class=\"u-js-hide\"><a href=\"#Aff2\">2</a>,<a href=\"#Aff4\">4</a><span itemprop=\"affiliation\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Organization\" class=\"u-visually-hidden\"><meta itemprop=\"name\" content=\"Cornell University\" /><meta itemprop=\"address\" content=\"grid.5386.8, 000000041936877X, Tri-Institutional Computational Biology &amp; Medicine Program, Cornell University, 1300 York Ave, New York, 10065, NY, USA\" /></span><span itemprop=\"affiliation\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Organization\" class=\"u-visually-hidden\"><meta itemprop=\"name\" content=\"Weill Cornell Medicine\" /><meta itemprop=\"address\" content=\"grid.5386.8, 000000041936877X, Department of Physiology and Biophysics, Institute for Computational Biomedicine, Weill Cornell Medicine, 1300 York Ave, New York, 10065, NY, USA\" /></span></sup>, </li><li class=\"c-author-list__item\" itemprop=\"author\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\"><a data-test=\"author-name\" data-track=\"click\" data-track-action=\"open author\" data-track-category=\"article body\" data-track-label=\"link\" href=\"#auth-3\">Ezgi Ebren</a></span><sup class=\"u-js-hide\"><a href=\"#Aff1\">1</a><span itemprop=\"affiliation\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Organization\" class=\"u-visually-hidden\"><meta itemprop=\"name\" content=\"Bilkent University\" /><meta itemprop=\"address\" content=\"grid.18376.3b, 0000 0001 0723 2427, Department of Computer Engineering, Bilkent University, Ankara, 06800, Turkey\" /></span></sup>, </li><li class=\"c-author-list__item\" itemprop=\"author\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\"><a data-test=\"author-name\" data-track=\"click\" data-track-action=\"open author\" data-track-category=\"article body\" data-track-label=\"link\" href=\"#auth-4\">Marzieh Eslami Rasekh</a></span><sup class=\"u-js-hide\"><a href=\"#Aff3\">3</a><span itemprop=\"affiliation\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Organization\" class=\"u-visually-hidden\"><meta itemprop=\"name\" content=\"Boston University\" /><meta itemprop=\"address\" content=\"grid.189504.1, 0000 0004 1936 7558, Graduate Program in Bioinformatics, Boston University, 24 Cummington Mall, Boston, 02215, MA, USA\" /></span></sup>, </li><li class=\"c-author-list__item\" itemprop=\"author\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\"><a data-test=\"author-name\" data-track=\"click\" data-track-action=\"open author\" data-track-category=\"article body\" data-track-label=\"link\" href=\"#auth-5\" data-corresp-id=\"c1\">Iman Hajirasouliha<svg width=\"16\" height=\"16\" class=\"u-icon\"><use xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:href=\"#global-icon-email\"></use></svg></a></span><sup class=\"u-js-hide\"><a href=\"#Aff4\">4</a>,<a href=\"#Aff5\">5</a><span itemprop=\"affiliation\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Organization\" class=\"u-visually-hidden\"><meta itemprop=\"name\" content=\"Weill Cornell Medicine\" /><meta itemprop=\"address\" content=\"grid.5386.8, 000000041936877X, Department of Physiology and Biophysics, Institute for Computational Biomedicine, Weill Cornell Medicine, 1300 York Ave, New York, 10065, NY, USA\" /></span><span itemprop=\"affiliation\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Organization\" class=\"u-visually-hidden\"><meta itemprop=\"name\" content=\"Weill Cornell Medicine\" /><meta itemprop=\"address\" content=\"grid.5386.8, 000000041936877X, Englander Institute for Precision Medicine, The Meyer Cancer Center, Weill Cornell Medicine, 1300 York Ave, New York, 10065, NY, USA\" /></span></sup> &amp; </li><li class=\"c-author-list__item\" itemprop=\"author\" itemscope=\"itemscope\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\"><a data-test=\"author-name\" data-track=\"click\" data-track-action=\"open author\" data-track-category=\"article body\" data-track-label=\"link\" href=\"#auth-6\" data-corresp-id=\"c2\">Can Alkan<svg width=\"16\" height=\"16\" class=\"u-icon\"><use xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:href=\"#global-icon-email\"></use></svg></a>
    

    【讨论】:

    • regex 将返回所有字符,包括“脏”HTML 代码。建议改用rvest
    猜你喜欢
    • 1970-01-01
    • 2018-04-30
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多