【问题标题】:Way to see if website was updated with rvest?如何查看网站是否使用 rvest 进行了更新?
【发布时间】:2021-08-10 22:38:33
【问题描述】:

我正在尝试通过网络搜索 Nike,看看新运动鞋何时会掉落。我对网络抓取比较陌生,想知道是否有一种简单的方法可以检查上次搜索中的差异或提取有关产品发布日期的信息。

到目前为止,我已经能够通过抓取按最新排序的新到货页面来提取最新产品的列表,但似乎无法在该页面上找到有关商品发布时间的信息。

library(rvest)
library(tidyverse)
url<-"https://www.nike.com/w/new-mens-shoes-3n82yznik1zy7ok?sort=newest"
search<-read_html(url)
search%>%html_nodes(css ="div.product-card")%>%html_text()

感谢任何提示。

【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    将列表保存到本地驱动器的最简单方法,然后每次执行查询时将新获取的列表与之前找到的列表进行比较。

    在这里,我创建了一个包含今天日期和查询的数据框,并将其保存到名为“Nike.csv”的文件中。然后我将运行这个脚本,确定新添加的并追加到现有文件。然后,您可以打开 csv 文件并查看每只鞋添加到列表中的日期。

    library(rvest)
    library(dplyr)
    
    #Read file of previous found shoes
    existing <- read.csv("Nike.csv")
    
    #Retrieve the latest list
    url<-"https://www.nike.com/w/new-mens-shoes-3n82yznik1zy7ok?sort=newest"
    search<-read_html(url)
    shoes <- search%>%html_nodes(css ="div.product-card")%>%html_text()
    
    #Find the differences between the latest list and previous found list
    newshoes <- !(shoes %in% existing$shoes)
    
    #Append to the differences to the current list
    if (length(shoes[newshoes])>0) {
       df<-data.frame(date = Sys.Date(), shoes[newshoes])
       write.csv(df, "Nike.csv", row.names = FALSE, append = TRUE,)
    } else {
       print("No new records found")
    }
    

    有一些方法可以优化这一点并改进错误检查,但这会让你开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 2022-08-04
      • 2014-02-03
      • 2013-01-04
      • 1970-01-01
      相关资源
      最近更新 更多