r - 如何根据标题抓取具有不同 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 的数据。
解决方案
好吧,似乎需要一些数据清理步骤。我强烈推荐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"
访问此官方文档以获取更多信息。我希望你觉得这有帮助。
推荐阅读
- ruby-on-rails - Rails 5 API - has_many 通过创建操作返回 2 条记录,尽管只有 1 条记录保存在数据库中
- git - 签出较早的提交后无法看到新的 Git 提交
- sql - SQL Server 2016 - 从单个表将逻辑层次结构构建到 JSON 结构中
- java - 无法使用 jersey 和 java 在 post 请求中解析字符串 JSON
- python - 无法在 Django 中将对象列表序列化为 JSON
- javascript - 缺少 3+n 个值的对象子数组(基于前 2 个值)
- mysql - 当基于角色的访问控制不够时,如何为用户分配角色和权限?
- python - 无法修改python中的numpy数组
- neo4j - 在 Neo4J 的 Jaccard 相似性查询中添加 CASE WHEN 条件
- dart - 如何在角度飞镖项目中设置路线上的活动导航链接?