【问题标题】:How can I scrape Informations from a Website, that are constantly updated?如何从网站上抓取不断更新的信息?
【发布时间】:2020-01-09 06:00:26
【问题描述】:

我正在尝试获取有关停车场未占用停车位的信息。网站上的信息不断更新免费停车位的数量。

自从我开始学习使用 R 进行网页抓取后,我开始学习基础知识。 所以我尝试使用代码获取 IMDB 电影的年份

url2 <- "https://www.imdb.com/search/title/?count=100&release_date=2016,2016&title_type=feature"
page2 <- read_html(url2)

data2 <- page2 %>%
  html_node(".lister-item-year") %>%
  html_text
data2

这段代码运行没有问题。

现在我在关于停车位的网站上进行了同样的尝试,由于 HTML 代码与上面示例中的几乎相同,我认为应该不会那么难。

url <- "https://www.rosenheim.de/stadt-buerger/verkehr/parken.html"

page <- read_html(url)

data <- page %>%
  html_node('.jwGetFreeParking-8') %>%
  html_text
data

但结果我没有得到有关免费停车位的信息。我得到的结果是“”。所以没什么。

是不是因为第二个网页上的数字不定时更新?

【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    此页面是使用 javascript 呈现的,因此您的示例中的技术不适用。如果您使用浏览器中的开发人员工具并检查加载在网络选项卡上的文件,您会发现一个名为“index.php”的文件。这是一个包含停车信息的 JSON 文件。

    下载此文件将提供所需的信息。 “jsonlite”库的fromJSON函数将访问该文件并将其转换为数据框。

    library(jsonlite)
    
    answer<-fromJSON("https://www.rosenheim.de/index.php?eID=jwParkingGetParkings")
    answer
    
       uid                     title parkings occupied free isOpened link
    1    4                   Reserve        0        0  ---    FALSE    0
    2    7                   Reserve        0        0  ---    FALSE    0
    3   13                   Reserve        0        0  ---    FALSE    0
    4   14                   Reserve        0        0  ---    FALSE    0
    5    0                P1 Zentrum      257      253    4     TRUE  224
    6    1                  P2 KU'KO      138      133    5     TRUE  225
    7    2                P3 Rathaus       31       29    2     TRUE  226
    8    3                  P4 Mitte      275      275    0     TRUE  227
    9    5             P6 Salinplatz      232      148   84     TRUE  228
    10   6           P7 Altstadt-Ost       82      108    0     TRUE  229
    11  10      P8 Beilhack-Citydome      160      130   30     TRUE  230
    12   8            P9 Am Klinikum      426      424    2     TRUE 1053
    13   9           P10 Stadtcenter       56       54    2     TRUE  231
    14  11 P11 Beilhack-Gießereistr.      155      155  ---    FALSE 1151
    15  12          P12 Bahnhof Nord      148       45  103     TRUE 1203
    

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      相关资源
      最近更新 更多