R의 Google 뉴스

계정

Google 뉴스에서 정보를 얻으려고합니다. 이것은 내 코드입니다.

library(rvest)
library(tidyverse)


news <- function(term) {

  html_dat <- read_html(paste0("https://news.google.com/search?q=",term,"&hl=es-419&gl=US&ceid=US%3Aes-419")) 

  dat <- data.frame(Link = html_dat %>%
                      html_nodes('.VDXfz') %>% 
                      html_attr('href')) %>% 
    mutate(Link = gsub("./articles/","https://news.google.com/articles/",Link))

  news_dat <- data.frame(
    Title = html_dat %>%
      html_nodes('.DY5T1d') %>% 
      html_text(),
    Link = dat$Link,
    Description =  html_dat %>%
      html_nodes('.Rai5ob') %>% 
      html_text()
  )

  return(news_dat)
}

noticias<-news("coronavirus")

이 코드를 사용하여 제목, 링크 및 설명을 검색합니다. 확인. 하지만 2 개의 필드가 더 필요합니다 : 날짜와 미디어. 예를 들어, 어제 코로나 바이러스 백신에 대한 뉴스가 발표 된 경우 날짜는 그 날짜가됩니다. 미디어가 New York Times 인 경우이 필드가 바로 그것입니다. 그러나 HTML에서 이러한 노드를 찾지 못했습니다. 이 두 필드를 추가하여 코드를 수정하는 방법이 있습니까?

미리 감사드립니다.

에코 암

아마도 이것을 시도

news <- function(term) {
  url <- paste0("https://news.google.com/search?q=", term, "&hl=es-419&gl=US&ceid=US:es-419")
  nodeset <- read_html(url) %>% html_nodes("article")
  tibble::tibble(
    Title = nodeset %>% html_nodes("h3") %>% html_text(), 
    Link = nodeset %>% html_nodes("h3 > a") %>% html_attr("href") %>% xml2::url_absolute(url), 
    Description = nodeset %>% html_nodes("div.Da10Tb.Rai5ob > span") %>% html_text(), 
    Source = nodeset %>% html_nodes("div.QmrVtf.RD0gLb.kybdz > div > a") %>% html_text(), 
    Time = nodeset %>% html_nodes("div.QmrVtf.RD0gLb.kybdz > div > time") %>% html_attr("datetime")
  )
}

산출

> news("coronavirus")
# A tibble: 100 x 5
   Title                                Link                                           Description                                        Source   Time     
   <chr>                                <chr>                                          <chr>                                              <chr>    <chr>    
 1 India reporta 41.100 casos nuevos d~ https://news.google.com/articles/CBMikwFodHRw~ "NUEVA DELHI (AP) — India reportó el domingo 41.1~ La Voz ~ 2020-11-~
 2 El ecuatoriano Diego Palacios, de L~ https://news.google.com/articles/CBMigwFodHRw~ "El defensa del LAFC, Diego Palacios, se encuentr~ ESPN De~ 2020-11-~
 3 Coronavirus: Austria endurece medid~ https://news.google.com/articles/CAIiEL2L0sxq~ "El canciller Sebastian Kurz pidió a la población~ DW (Esp~ 2020-11-~
 4 ++Coronavirus hoy: Gobierno alemán ~ https://news.google.com/articles/CAIiEKCZppoU~ "\"Todos los países que levantaron sus restriccio~ DW (Esp~ 2020-11-~
 5 ++Coronavirus hoy++ México supera e~ https://news.google.com/articles/CAIiEK8ndryG~ "El COVID-19 se consolidó como la cuarta causa de~ DW (Esp~ 2020-11-~
 6 Coronavirus en Estados Unidos: 5 ci~ https://news.google.com/articles/CAIiEFFHgJgZ~ "La incertidumbre política y la emergencia sanita~ BBC New~ 2020-11-~
 7 México supera el millón de casos de~ https://news.google.com/articles/CBMiRWh0dHBz~ "México sobrepasó el millón de casos confirmados ~ Reuters~ 2020-11-~
 8 Massachusetts reporta 2.800 casos d~ https://news.google.com/articles/CBMiXmh0dHA6~ "Los casos registrados en la más reciente jornada~ El Tiem~ 2020-11-~
 9 ¿Qué hará NYC para resistir una seg~ https://news.google.com/articles/CBMifWh0dHBz~ "Reaccionan políticos locales a la orden de cerra~ NY1 Not~ 2020-11-~
10 + Coronavirus hoy: Italia suma 544 ~ https://news.google.com/articles/CAIiEJ4KB7k2~ "Argentina registró este sábado (14.11.2020) 8.46~ DW (Esp~ 2020-11-~
# ... with 90 more rows

최신 정보

다음과 같은 경우는 생각하지 못했습니다.

  1. 중첩 된 기사.

중첩

  1. 날짜-시간 속성이 없습니다.

날짜 없음

이 모든 경우를 설명하기 위해 코드를 업데이트했지만 코드의 효율성이 훨씬 떨어졌습니다. 어쨌든, 이것을 시도하십시오 :

news <- function(term) {
  url <- paste0("https://news.google.com/search?q=", term, "&hl=es-419&gl=US&ceid=US:es-419")
  nodeset <- read_html(url) %>% html_nodes("article")
  dplyr::bind_rows(lapply(nodeset, function(x) tibble::tibble(
    Title = x %>% html_node(".ipQwMb.ekueJc.RD0gLb") %>% html_text(), 
    Link = x %>% html_node(".ipQwMb.ekueJc.RD0gLb > a") %>% html_attr("href") %>% xml2::url_absolute(url), 
    Description = x %>% html_node("div.Da10Tb.Rai5ob > span") %>% html_text(), 
    Source = x %>% html_node("div.QmrVtf.RD0gLb.kybdz > div > a") %>% html_text(), 
    Time = x %>% html_node("div.QmrVtf.RD0gLb.kybdz > div > time") %>% html_attr("datetime")
  )))
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Google 뉴스 스크래핑

분류에서Dev

RSS Google 뉴스 언어

분류에서Dev

Google 뉴스 사이트 맵

분류에서Dev

이 Google 스프레드 시트의 메뉴를 Google 문서 도구로 변환하는 방법

분류에서Dev

Google 드라이브 폴더 외부의 Google 드라이브 컨텍스트 메뉴 비활성화

분류에서Dev

지도 / 뉴스의 Google 카드와 같은 Android 용 xamarin (Visual Studio)의 카드 스 와이프

분류에서Dev

WebView의 뉴스 링크

분류에서Dev

Google 문서 (스프레드 시트 없음)의 링크를 메뉴로 직접 열고 싶습니다.

분류에서Dev

Google 크롬의 비밀 메뉴

분류에서Dev

14.04 용 Google 크롬의 빈 메뉴 바

분류에서Dev

내 Google Glass 앱의 유사한 Google Glass 메뉴

분류에서Dev

gnome-terminal에서 선택한 텍스트의 컨텍스트 메뉴에서 "Google 검색"을 사용하는 방법은 무엇입니까?

분류에서Dev

gnome-terminal에서 선택한 텍스트의 컨텍스트 메뉴에서 "Google 검색"을 사용할 수있게하는 방법은 무엇입니까?

분류에서Dev

Google 크롬에서 컨텍스트 메뉴의 배경색을 스타일링하기위한 매개 변수 이름은 무엇입니까?

분류에서Dev

행 수가 다른 Rvest 스크랩 Google 뉴스

분류에서Dev

행 수가 다른 Rvest 스크랩 Google 뉴스

분류에서Dev

lxml 및 python으로 Google 뉴스 스크랩

분류에서Dev

SOM 뉴런의 클러스터링

분류에서Dev

Linux의 QT 메뉴 창 스타일

분류에서Dev

QGraphicsItemGroup qt의 컨텍스트 메뉴

분류에서Dev

메뉴의 텍스트가 옆으로

분류에서Dev

메뉴의 스타일 화살표

분류에서Dev

Yii의 데이터베이스 메뉴

분류에서Dev

조각 컨텍스트 메뉴의 ListView

분류에서Dev

여러 페이지의 Joomla 뉴스

분류에서Dev

데이터베이스의 Codeigniter 메뉴

분류에서Dev

SaveFileDialog의 컨텍스트 메뉴 버그

분류에서Dev

Ckfinder의 컨텍스트 메뉴

분류에서Dev

스위프트의 메인 메뉴

Related 관련 기사

  1. 1

    Google 뉴스 스크래핑

  2. 2

    RSS Google 뉴스 언어

  3. 3

    Google 뉴스 사이트 맵

  4. 4

    이 Google 스프레드 시트의 메뉴를 Google 문서 도구로 변환하는 방법

  5. 5

    Google 드라이브 폴더 외부의 Google 드라이브 컨텍스트 메뉴 비활성화

  6. 6

    지도 / 뉴스의 Google 카드와 같은 Android 용 xamarin (Visual Studio)의 카드 스 와이프

  7. 7

    WebView의 뉴스 링크

  8. 8

    Google 문서 (스프레드 시트 없음)의 링크를 메뉴로 직접 열고 싶습니다.

  9. 9

    Google 크롬의 비밀 메뉴

  10. 10

    14.04 용 Google 크롬의 빈 메뉴 바

  11. 11

    내 Google Glass 앱의 유사한 Google Glass 메뉴

  12. 12

    gnome-terminal에서 선택한 텍스트의 컨텍스트 메뉴에서 "Google 검색"을 사용하는 방법은 무엇입니까?

  13. 13

    gnome-terminal에서 선택한 텍스트의 컨텍스트 메뉴에서 "Google 검색"을 사용할 수있게하는 방법은 무엇입니까?

  14. 14

    Google 크롬에서 컨텍스트 메뉴의 배경색을 스타일링하기위한 매개 변수 이름은 무엇입니까?

  15. 15

    행 수가 다른 Rvest 스크랩 Google 뉴스

  16. 16

    행 수가 다른 Rvest 스크랩 Google 뉴스

  17. 17

    lxml 및 python으로 Google 뉴스 스크랩

  18. 18

    SOM 뉴런의 클러스터링

  19. 19

    Linux의 QT 메뉴 창 스타일

  20. 20

    QGraphicsItemGroup qt의 컨텍스트 메뉴

  21. 21

    메뉴의 텍스트가 옆으로

  22. 22

    메뉴의 스타일 화살표

  23. 23

    Yii의 데이터베이스 메뉴

  24. 24

    조각 컨텍스트 메뉴의 ListView

  25. 25

    여러 페이지의 Joomla 뉴스

  26. 26

    데이터베이스의 Codeigniter 메뉴

  27. 27

    SaveFileDialog의 컨텍스트 메뉴 버그

  28. 28

    Ckfinder의 컨텍스트 메뉴

  29. 29

    스위프트의 메인 메뉴

뜨겁다태그

보관