2

我有兴趣提取新闻中报道的超自然活动数据,以便我可以分析出现的空间和时间数据是否存在任何相关性。这个项目只是为了好玩,学习和使用网络抓取、文本提取和空间和时间相关性分析。所以请原谅我决定这个话题,我想做一些有趣和具有挑战性的工作。首先我发现这个网站收集了一些报告的超自然事件,他们收集了 2009、2010、2011 和 2012 年的数据。网站的结构是这样的,每年有 1..10 个页面......并且链接去像这个 2009 年链接http://paranormal.about.com/od/paranormalgeneralinfo/tp/2009-paranormal-activity.htm

在每个页面中,他们都收集了标题下的故事,如内部结构超自然活动,发布于 09 年 3 月 14 日,每个标题行都有两个页面......像这个链接一样http://paranormal.about.com/ od/paranormalgeneralinfo/a/news_090314n.htm

在这些页面中的每一个上,他们都在各种标题上收集了实际报道的故事……以及这些故事的实际网站链接。我有兴趣收集那些报告的文本,并提取有关鬼魂、恶魔或不明飞行物等超自然活动的类型以及事件发生的时间、日期和地点的信息。我希望分析这些数据的任何空间和时间相关性。如果 UFO 或幽灵是真实的,它们的运动中一定有一些空间或时间的行为和相关性。这是故事的长镜头...

我需要帮助来抓取上述页面的文本。在这里,我已经写下代码以跟随一页并将其链接到我想要的最后一个最终文本。谁能告诉我有没有更好更有效的方法来从最后一页获取干净的文本。此外,通过跟踪整个 2009 年的所有 10 页来自动收集文本。

library(XML)
#source of paranormal news from about.com
#first page to start
#2009 -  http://paranormal.about.com/od/paranormalgeneralinfo/tp/2009-paranormal-activity.htm
pn.url<-"http://paranormal.about.com/od/paranormalgeneralinfo/tp/2009-paranormal-activity.htm"
pn.html<-htmlTreeParse(pn.url,useInternalNodes=T)
pn.h3=xpathSApply(pn.html,"//h3",xmlValue)
#extracting the links of the headlines to follow to the story
pn.h3.links=xpathSApply(pn.html,"//h3/a/@href")
#Extracted the links of the Internal structure to follow ...
#Paranormal Activity, Posted 01-03-09 (following this head line)
#http://paranormal.about.com/od/paranormalgeneralinfo/a/news_090314n.htm
pn.l1.url<-pn.h3.links[1]
pn.l1.html<-htmlTreeParse(pn.l1.url,useInternalNodes=T)
pn.l1.links=xpathSApply(pn.l1.html,"//p/a/@href")
#Extracted the links of the Internal structure to follow ...
#British couple has 'black-and-white-twins' twice (following this head line)
#http://www.msnbc.msn.com/id/28471626/
pn.l1.f1.url=pn.l1.links[7]
pn.l1.f1.html=htmlTreeParse(pn.l1.f1.url,useInternalNodes=T)
pn.l1.f1.text=xpathSApply(pn.l1.f1.html,"//text()[not(ancestor::script)][not(ancestor::style)]",xmlValue)

我衷心感谢您阅读我的帖子以及您为我提供帮助的时间。对于任何想在整个项目中指导我的专家,我都会非常满意。

问候萨蒂什

4

1 回答 1

0

尝试使用ScrapyBeautifulSoup库。尽管它们是基于 Python 的,但它们被认为是报废领域中最好的。您可以使用命令行界面来连接两者,有关连接 R 和 Python 的更多详细信息,请查看此处

于 2016-03-08T18:06:42.580 回答