简体   繁体   English

如何遍历R中的多个URL并保存在数据框中

[英]How to loop through multiple URLs in R and save in data frame

I have been unable to loop through multiple URLs and have it save in a data frame.我无法遍历多个 URL 并将其保存在数据框中。 I have shared the code that can retrieve only one url at a time and save in a data frame.我分享了一次只能检索一个 url 并保存在数据框中的代码。

The part of the url that changes is a number at the very end of the url that refers to a date. url 中更改的部分是 url 末尾的一个数字,它指的是日期。 I am trying to scrape all the data from, for example, 20190901 through 20190915 and store it in the same data frame.我正在尝试从例如 20190901 到 20190915 中抓取所有数据并将其存储在同一个数据框中。

Here is the Code:这是代码:

    library(rvest)
    library(dplyr)

    # Specifying URL
    url <- 'https://classic.sportsbookreview.com/betting-odds/mlb-baseball/?date=20190901'
    
    # Reading the HTML code from website
    oddspage <- read_html(url)

    # Using CSS selectors to scrape away teams
    awayHtml <- html_nodes(oddspage,'.eventLine-value:nth-child(1) a')

    #Using CSS selectors to scrape scores
    awayScoreHtml <- html_nodes(oddspage,'.first.total')
    awayScore <- html_text(awayScoreHtml)
    awayScore <- as.numeric(awayScore)
    homeScoreHtml <- html_nodes(oddspage, '.score-periods+ .score-periods .total')
    homeScore <- html_text(homeScoreHtml)
    homeScore <- as.numeric(homeScore)

    # Converting away data to text
    away <- html_text(awayHtml)

    # Using CSS selectors to scrape home teams
    homeHtml <- html_nodes(oddspage,'.eventLine-value+ .eventLine-value a')

    # Converting home data to text
    home <- html_text(homeHtml)

    # Using CSS selectors to scrape Away Odds
    awayPinnacleHtml <- html_nodes(oddspage,'.eventLine-consensus+ .eventLine-book.eventLine-book-value:nth-child(1) b')
    awayBookmakerHtml <- html_nodes(oddspage,'.eventLine-book:nth-child(12) .eventLine-book-value:nth-child(1) b')

    # Converting Away Odds to Text
    awayPinnacle <- html_text(awayPinnacleHtml)
    awayBookmaker <- html_text(awayBookmakerHtml)

    # Converting Away Odds to numeric
    awayPinnacle <- as.numeric(awayPinnacle)
    awayBookmaker <- as.numeric(awayBookmaker)

    # Using CSS selectors to scrape Pinnacle Home Odds
    homePinnacleHtml <- html_nodes(oddspage,'.eventLine-consensus+ .eventLine-book .eventLine-book-value+ .eventLine-book-value b')
    homeBookmakerHtml <- html_nodes(oddspage,'.eventLine-book:nth-child(12) .eventLine-book-value:nth-child(2) b')

    # Converting Home Odds to Text
    homePinnacle <- html_text(homePinnacleHtml)
    homeBookmaker <- html_text(homeBookmakerHtml)

    # Converting Home Odds to Numeric
    homePinnacle <- as.numeric(homePinnacle)
    homeBookmaker <- as.numeric(homeBookmaker)


    # Create Data Frame
    df <- data.frame(away,home,awayScore,homeScore,awayPinnacle,homePinnacle,awayBookmaker,homeBookmaker)

    View(df)

I am very new to coding and I have not been able to successfully apply any of the techniques used in similar questions.我对编码非常陌生,并且无法成功应用类似问题中使用的任何技术。

Put all your code in a function and make date dynamic to generate url:将所有代码放入 function 并动态生成date以生成 url:

get_data <- function(date) {
      url <- paste0('https://classic.sportsbookreview.com/betting-odds/mlb-baseball/?date=', date)
      #...Rest of the code as it is
      #...
}

Create a vector of dates using sprintf使用sprintf创建日期向量

date_vec <- sprintf('201909%02d', 1:15)
date_vec
# [1] "20190901" "20190902" "20190903" "20190904" "20190905" "20190906"
# [7] "20190907" "20190908" "20190909" "20190910" "20190911" "20190912"
#[13] "20190913" "20190914" "20190915"

Use lapply to extract data for each date and combine them.使用lapply提取每个日期的数据并将它们组合起来。

all_data <- do.call(rbind, lapply(date_vec, get_data))

You can also use map_df from purrr .您也可以使用map_df中的purrr

all_data <- purrr::map_df(date_vec, get_data)

However, you might need to add some checks in your function for pages which don't return any values for a particular field.但是,您可能需要在 function 中为不返回特定字段的任何值的页面添加一些检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM