繁体   English   中英

如何使用readLines和grep在R中构建webscraper?

[英]how to build a webscraper in R using readLines and grep?

我是R的新手。我想编写一份100万字的报纸文章。 因此,我正在尝试编写一个网络刮刀来检索来自监护人网站的报纸文章: http//www.guardian.co.uk/politics/2011/oct/31/nick-clegg-investment-new-jobs

刮刀用于从一页开始,检索文章的正文,删除所有标签并将其保存到文本文件中。 然后它应该通过本页面上的链接转到下一篇文章,获取文章等,直到该文件包含大约100万字。

不幸的是,我的刮刀并没有走得太远。

我使用readLines()来访问网站的源代码,现在想要获取代码中的相关行。

Guardian中的相关部分使用此ID来标记文章的正文:

<div id="article-body-blocks">         
  <p>
    <a href="http://www.guardian.co.uk/politics/boris"
       title="More from guardian.co.uk on Boris Johnson">Boris Johnson</a>,
       the...a different approach."
  </p>
</div>

我尝试使用grep和lookbehind的各种表达式来掌握这一部分 - 尝试获取此ID后面的行 - 但我认为它不适用于多行。 至少我不能让它发挥作用。

有人可以帮忙吗? 如果有人可以提供一些我可以继续工作的代码,那将是很棒的!

谢谢。

如果您真的坚持使用grepreadLines ,那么您将面临清理已删除页面的问题,但这当然可以完成。 例如。:

加载页面:

html <- readLines('http://www.guardian.co.uk/politics/2011/oct/31/nick-clegg-investment-new-jobs')

stringr包的str_extract和简单的正则表达式的帮助下,你完成了:

library(stringr)
body <- str_extract(paste(html, collapse='\n'), '<div id="article-body-blocks">.*</div>')

好吧, body看起来很难看,你必须从<p>和脚本中清理它。 这可以通过gsub和朋友(很好的正则表达式)来完成。 例如:

gsub('<script(.*?)script>|<span(.*?)>|<div(.*?)>|</div>|</p>|<p(.*?)>|<a(.*?)>|\n|\t', '', body)

正如@Andrie建议的那样,你应该使用一些为此目的构建的包。 小演示:

library(XML)
library(RCurl)
webpage <- getURL('http://www.guardian.co.uk/politics/2011/oct/31/nick-clegg-investment-new-jobs')
webpage <- readLines(tc <- textConnection(webpage)); close(tc)
pagetree <- htmlTreeParse(webpage, useInternalNodes = TRUE, encoding='UTF-8')
body <- xpathSApply(pagetree, "//div[@id='article-body-blocks']/p", xmlValue)

body导致文本干净的地方:

> str(body)
 chr [1:33] "The deputy prime minister, Nick Clegg, has said the government's regional growth fund will provide a \"snowball effect that cre"| __truncated__ ...

更新 :以上作为单行(感谢@Martin Morgan的建议):

xpathSApply(htmlTreeParse('http://www.guardian.co.uk/politics/2011/oct/31/nick-clegg-investment-new-jobs', useInternalNodes = TRUE, encoding='UTF-8'), "//div[@id='article-body-blocks']/p", xmlValue)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM