使用R从网页中获取所有搜索结果,当它';启用了JavaScript

使用R从网页中获取所有搜索结果,当它';启用了JavaScript,javascript,html,r,v8,Javascript,Html,R,V8,我不熟悉网络抓取,我正在尝试使用R从以下网站获取搜索结果: 很容易获得结果的第一页,例如使用 library(rvest) library(V8) link <- 'https://devtracker.fcdo.gov.uk/sector/1/projects' devtracker = read_html(link) searchresults = devtracker %>% html_nodes(xpath = '//div[@class="se

我不熟悉网络抓取,我正在尝试使用R从以下网站获取搜索结果:

很容易获得结果的第一页,例如使用

library(rvest)
library(V8)
link <- 'https://devtracker.fcdo.gov.uk/sector/1/projects'

devtracker = read_html(link)        
searchresults = devtracker %>% html_nodes(xpath = '//div[@class="search-result"]//a',) %>% html_attr('href')
应该找到指向其他页面的链接,但这将返回一个空节点集。我知道这与使用JavaScript作为页面列表的页面有关,我尝试在下面的博客中使用V8:但不知道如何使用它显示下一页的结果


对于新手来说,有没有一种简单的方法可以从这个页面获取所有的搜索结果?

可能有更好的选择。对HTTP请求的快速检查表明,HTML是从JSON数据呈现的,您可以直接访问JSON数据:

https://devtracker.fcdo.gov.uk/api/activities/?hierarchy=1&page_size=200&format=json&reporting_organisation_identifier=GB-GOV-15,GB-GOV-9,GB-GOV-6,GB-GOV-2,GB-GOV-1,GB-1,GB-GOV-3,GB-GOV-13,GB-GOV-7,GB-GOV-50,GB-GOV-52,GB-6,GB-10,GB-GOV-10,GB-9,GB-GOV-8,GB-GOV-5,GB-GOV-12,GB-COH-RC000346,GB-COH-03877777&fields=activity_dates,aggregations,activity_status,id,iati_identifier,url,title,reporting_organisation,activity_plus_child_aggregation,descriptions&activity_status=2&ordering=-activity_plus_child_budget_value&total_hierarchy_budget_gte=&total_hierarchy_budget_lte=&actual_start_date_gte=&planned_end_date_lte=&sector=&related_activity_sector=11110,11120,11130,11182,11220,11230,11240,11320,11330,11420,11430,11231,11232,11321,11322,&recipient_country=&recipient_region=&document_link_category=&participating_organisation=&page=1&format=json
您可以更改
page\u size
page
参数,以便在一个请求中获取所有内容,然后使用
jsonlite
或类似的包进行解析

https://devtracker.fcdo.gov.uk/api/activities/?hierarchy=1&page_size=200&format=json&reporting_organisation_identifier=GB-GOV-15,GB-GOV-9,GB-GOV-6,GB-GOV-2,GB-GOV-1,GB-1,GB-GOV-3,GB-GOV-13,GB-GOV-7,GB-GOV-50,GB-GOV-52,GB-6,GB-10,GB-GOV-10,GB-9,GB-GOV-8,GB-GOV-5,GB-GOV-12,GB-COH-RC000346,GB-COH-03877777&fields=activity_dates,aggregations,activity_status,id,iati_identifier,url,title,reporting_organisation,activity_plus_child_aggregation,descriptions&activity_status=2&ordering=-activity_plus_child_budget_value&total_hierarchy_budget_gte=&total_hierarchy_budget_lte=&actual_start_date_gte=&planned_end_date_lte=&sector=&related_activity_sector=11110,11120,11130,11182,11220,11230,11240,11320,11330,11420,11430,11231,11232,11321,11322,&recipient_country=&recipient_region=&document_link_category=&participating_organisation=&page=1&format=json