首页 > 解决方案 > 如何根据标题抓取具有不同 URL 的多个页面的网页数据?

问题描述

我正在从 URL http://iias.ac.in/recent-publications抓取网络数据。我已经使用“rvest”抓取了该页面所有标题的数据。现在我有一个包含书名的向量

titl_book [1] “泰戈尔的一些论文:历史。社会。政治”
[2] “不可见的网络:对 Jangarh Singh Shyam 生与死的艺术历史调查” ..

现在我正在抓取每本书的数据,其 url 是基于书名的,比如 http://iias.ac.in/publication/some-essays-tagore-history-society-politics

作为矢量 title_book 包含公共 url “http://iias.ac.in”的后缀,如何一次性抓取所有此类 URL 的数据。

标签: rweb-scrapingrvest

解决方案


好吧,似乎需要一些数据清理步骤。我强烈推荐stringr包裹。这是我将如何做到的。

title_book = c("Some Essays of Tagore : History. Society. Politics",
  "INVISIBLE WEBS: An art Historical inquiry into the life and death of Jangarh Singh Shyam")

title_book_edited = title_book %>% 
  str_to_lower() %>% 
  str_replace_all(pattern = " ", replacement = "-") %>% 
  str_remove_all(pattern = ":") %>% 
  str_remove_all(pattern = "\\.")

title_book_list = paste0("http://iias.ac.in/publication/", title_book_edited)

我曾经str_to_lower()转换字符串的大小写,str_replace_all()替换所有匹配的模式并str_remove_all()删除所有匹配的模式。输出看起来像这样。

> title_book_list
[1] "http://iias.ac.in/publication/some-essays-of-tagore--history-society-politics"                                        
[2] "http://iias.ac.in/publication/invisible-webs-an-art-historical-inquiry-into-the-life-and-death-of-jangarh-singh-shyam"

访问此官方文档以获取更多信息。我希望你觉得这有帮助。


推荐阅读