【问题标题】:Could you please help me with web scraping using Rvest?您能帮我使用 Rvest 进行网页抓取吗?
【发布时间】:2021-05-23 00:47:47
【问题描述】:

我目前正在尝试抓取以下网站:https://chicago.suntimes.com/crime/archives

我一直依靠 CSS 选择器小工具来查找 x-path 并进行网络抓取。但是,我无法使用本网站中的小工具,我必须使用 Inspect Source 来查找我需要的东西。我一直试图通过向下滚动每个源来找到相关的 css 和 xpath,但由于我的能力有限,我无法做到。

你能帮我找到xpath或css吗

  • 标题
  • 作者
  • 日期

如果这是所有东西的干洗清单,我很抱歉......但我真的被困住了。如果您能给我一些帮助,我将不胜感激!

非常感谢。

【问题讨论】:

  • 如果您无法使用 CSS 选择器小工具找到简单的东西,我强烈建议您深入研究开发人员工具。我花了大半天的时间在开发工具中找到特定的 XPATH。一旦你掌握了窍门,你就可以真正提高你的抓取能力,并且你独立于 Selector Gadget。此外,您将更好地了解您正在抓取的网站是如何构建的!玩得开心;)

标签: r screen-scraping rvest rselenium


【解决方案1】:

对于您想要提取的每个元素,如果您使用选择器小工具找到具有相应类的相关标签,您将能够得到您想要的。

library(rvest)
url <- 'https://chicago.suntimes.com/crime/archives'

webpage <- url %>% read_html() 
title <- webpage %>% html_nodes('h2.c-entry-box--compact__title') %>% html_text()
author <- webpage %>% html_nodes('span.c-byline__author-name') %>% html_text()
date <- webpage %>% html_nodes('time.c-byline__item')%>% html_text() %>% trimws()
result <- data.frame(title, author, date)
result

result
#                                                                                               title              author        date
#1                               Belmont Cragin man charged with carjacking in Little Village: police       Sun-Times Wire February 17
#2                                                   Gas station robbed, man carjacked in Horner Park       Jermaine Nolen February 17
#3                                                              8 shot, 2 fatally, Tuesday in Chicago       Sun-Times Wire February 17
#4                                        Businesses robbed at gunpoint on the Northwest Side: police       Sun-Times Wire February 17
#5                                                              Man charged with carjacking in Aurora       Sun-Times Wire February 16
#6                                                       Woman fatally stabbed in Park Manor apartment      Sun-Times Wire February 16
#7                                                        Woman critically hurt by gunfire in Woodlawn       David Struett February 16
#8                                Teen boy, 17, charged with attempted carjacking in Back of the Yards      Sun-Times Wire February 16
#...
#...

【讨论】:

  • 非常感谢!这真的很有帮助......!谢谢!!!!!!
猜你喜欢
  • 2019-10-11
  • 2018-02-15
  • 2018-10-27
  • 2015-09-06
  • 1970-01-01
相关资源
最近更新 更多