履带式刮擦不产生页面

时间:2018-07-31 12:40:59

标签: r web-scraping rcrawler

我正在使用Rcrawler提取Wikipedia页面的信息框。我有一个音乐家列表,我想提取他们的姓名,DOB,死亡日期,乐器,标签等。然后,我想创建列表中所有艺术家的数据行,并将其存储为列/向量。

下面的代码不会引发任何错误,但我也不会得到任何结果。当我自己使用rvest时,代码中使用的xpath有效。

我的代码有什么问题?

library(Rcrawler)
jazzlist<-c("Art Pepper","Horace Silver","Art Blakey","Philly Joe Jones")

Rcrawler(Website = "http://en.wikipedia.org/wiki/Special:Search/", no_cores = 4, no_conn = 4, 
     KeywordsFilter = jazzlist,
     ExtractXpathPat = c("//th","//tr[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]//td",
                         "//tr[(((count(preceding-sibling::*) + 1) = 6) and parent::*)]//td"),
     PatternsNames = c("artist", "dob", "dod"), 
     ManyPerPattern = TRUE, MaxDepth=1 )

2 个答案:

答案 0 :(得分:2)

从特定的维基百科网址列表中抓取数据

如果您想使用通用格式来抓取特定的URL列表,请使用ContentScraper函数:

library(Rcrawler)
jazzlist<-c("Art Pepper","Horace Silver","Art Blakey","Philly Joe Jones")
target_pages = paste0('https://en.wikipedia.org/wiki/Special:Search/', gsub(" ", "_", jazzlist))
DATA<-ContentScraper(Url = target_pages , 
                     XpathPatterns = c("//th","//tr[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]//td","//tr[(((count(preceding-sibling::*) + 1) = 6) and parent::*)]//td"),
                     PatternsName = c("artist", "dob", "dod"),
                     asDataFrame = TRUE)
View(DATA)

Scrape list of wikipedia URLS

从维基百科的链接列表中抓取和抓取数据

不费吹灰之力,我在Wikipedia中找到了硬跳音乐家列表,我想您会对抓取所有这些艺术家数据感兴趣;在这种情况下,我们将使用Rcrawler函数自动收集和解析所有这些页面。

Rcrawler(Website = "https://en.wikipedia.org/wiki/List_of_hard_bop_musicians" ,
         no_cores = 4, no_conn = 4, MaxDepth = 1, 
         ExtractXpathPat = c("//th","//tr[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]//td","//tr[(((count(preceding-sibling::*) + 1) = 6) and parent::*)]//td"),
         PatternsNames = c("artist", "dob", "dod"),
         crawlZoneXPath = "//*[@class='mw-parser-output']")
#transform data into dataframe
 df<-data.frame(do.call("rbind", DATA))
  • MaxDepth = 1:仅抓取起始页中的链接
  • crawlZoneXPath:仅抓取页面正文(艺术家列表)中的链接
  • ExtractXpathPat:要提取的数据的XPath模式

crawl wikipedia webpages

爬行者创建者

答案 1 :(得分:1)

我可能是错的,但是我怀疑您认为Rcrawler软件包的工作方式与它的工作方式有所不同。您可能将抓取与抓取混淆了。

抓取工具只是从给定页面开始,然后抓取该页面上的任何链接。您可以使用URL过滤器或关键字过滤器来缩小路径的范围,但是仍然需要通过爬网过程访问这些页面。它不会运行搜索。

您实际上是从Wikipedia搜索页面开始的,这表明您可能希望它以$('.nav li').each(function(){ var img_link = $(this).attr('data-menu'); var img_html = '<img src="'+img_link+'">'; // here I have to concatenate the img_html in gethtml variable and then I will put it in a div. }); $('.main-img').html(gethtml); 中指定的字词运行搜索,但不会这样做。它将仅跟踪Wikipedia搜索页面中的所有链接,例如左侧边栏中的“主页”,“内容”,“精选内容”,它最终可能会也可能不会与您使用的术语之一相抵触,在这种情况下,它将根据您的xpath参数抓取数据。 / p>

您指定的术语将非常少见,因此虽然最终可能会通过诸如“功能页面”之类的文章交叉链接找到它们,但这将花费非常长的时间。

我想您想要的是根本不使用Rcrawler,而是从搜索词的循环内调用jazzlist函数。您只需要将术语添加到您提到的搜索URL,然后用下划线替换空格:

rvest

编辑:根据他的评论,在下面添加了解决方案,并提供了OP具体案例的确切代码

library(rvest)
target_pages = paste0('https://en.wikipedia.org/wiki/Special:Search/', gsub(" ", "_", jazzlist))

for (url in target_pages){
    webpage = read_html(url)
    # do whatever else you want here with rvest functions 
}
相关问题