我是 R 的新手,希望能得到一些幫助。我正在嘗試從有關狗品種的網站上抓取資料。
品種串列的鏈接在這里:https : //dogtime.com/dog-breeds/profiles
每個單獨的品種組態檔的 url 都以https://dogtime.com/dog-breeds/為基礎,然后添加品種名稱(例如https://dogtime.com/dog-breeds/golden-retriever)。
我已經使用以下代碼成功抓取了一個品種的資料,但我現在想收集網站上所有 392 個品種的資料并將結果存盤在資料框中。
library(rvest)
library(dplyr)
library(purrr)
# Create a vector of URLs
dog_links <- page %>% html_nodes(".list-item-title") %>%
html_attr("href")
# Create a new variable for the website link
link = "https://dogtime.com/dog-breeds/golden-retriever"
# Get HTML code from this website
page <- read_html(link)
# Create variables for each of the attributes
breed <- page %>% html_nodes("h1") %>% html_text()
adaptability = page %>% html_nodes(".title-box .paws .parent-characteristic .characteristic-star-block") %>% html_text()
apartment_living = page %>% html_nodes(".title-box .paws .parent-characteristic .child-characteristic .characteristic-star-block") %>% html_text()
novice_owners = page %>% html_nodes(".title-box .paws .child-characteristic:nth-child(3) .characteristic-star-block") %>% html_text()
sensitivity_level = page %>% html_nodes(".title-box .paws .child-characteristic:nth-child(4) .characteristic-star-block") %>% html_text()
tolerates_alone = page %>% html_nodes(".title-box .paws .child-characteristic:nth-child(5) .characteristic-star-block") %>% html_text()
tolerates_cold = page %>% html_nodes(".title-box .paws .child-characteristic:nth-child(6) .characteristic-star-block") %>% html_text()
tolerates_hot = page %>% html_nodes(".title-box .paws .child-characteristic:nth-child(7) .characteristic-star-block") %>% html_text()
friendliness = page %>% html_nodes(".paws:nth-child(3) .parent-characteristic .characteristic-star-block") %>% html_text()
affectionate = page %>% html_nodes(".paws:nth-child(3) .parent-characteristic .child-characteristic .characteristic-star-block") %>% html_text()
kid_friendly = page %>% html_nodes(".paws:nth-child(3) .child-characteristic:nth-child(3) .characteristic-star-block") %>% html_text()
dog_friendly = page %>% html_nodes(".paws:nth-child(3) .child-characteristic:nth-child(4) .characteristic-star-block") %>% html_text()
stranger_friendly = page %>% html_nodes(".paws:nth-child(3) .child-characteristic:nth-child(5) .characteristic-star-block") %>% html_text()
health_grooming = page %>% html_nodes(".paws:nth-child(4) .parent-characteristic .characteristic-star-block") %>% html_text()
shedding = page %>% html_nodes(".paws:nth-child(4) .parent-characteristic .child-characteristic .characteristic-star-block") %>% html_text()
drooling = page %>% html_nodes(".paws:nth-child(4) .child-characteristic:nth-child(3) .characteristic-star-block") %>% html_text()
easy_groom = page %>% html_nodes(".paws:nth-child(4) .child-characteristic:nth-child(4) .characteristic-star-block") %>% html_text()
general_health = page %>% html_nodes(".paws:nth-child(4) .child-characteristic:nth-child(5) .characteristic-star-block") %>% html_text
weight_gain = page %>% html_nodes(".paws:nth-child(4) .child-characteristic:nth-child(6) .characteristic-star-block") %>% html_text()
size = page %>% html_nodes(".paws:nth-child(4) .child-characteristic:nth-child(7) .characteristic-star-block") %>% html_text()
trainability = page %>% html_nodes("#cf_hagn .paws .parent-characteristic .characteristic-star-block") %>% html_text()
easy_train = page %>% html_nodes("#cf_hagn .paws .parent-characteristic .child-characteristic .characteristic-star-block") %>% html_text()
intelligence = page %>% html_nodes("#cf_hagn .paws .child-characteristic:nth-child(3) .characteristic-star-block") %>% html_text()
mouthiness = page %>% html_nodes("#cf_hagn .paws .child-characteristic:nth-child(4) .characteristic-star-block") %>% html_text()
prey_drive = page %>% html_nodes("#cf_hagn .paws .child-characteristic:nth-child(5) .characteristic-star-block") %>% html_text()
barking = page %>% html_nodes("#cf_hagn .paws .child-characteristic:nth-child(6) .characteristic-star-block") %>% html_text()
wanderlust = page %>% html_nodes("#cf_hagn .paws .child-characteristic:nth-child(7) .characteristic-star-block") %>% html_text()
physical_needs = page %>% html_nodes("#cf_hagn~ .paws .paws .parent-characteristic .characteristic-star-block") %>% html_text()
energy_level = page %>% html_nodes("#cf_hagn~ .paws .paws .parent-characteristic .child-characteristic .characteristic-star-block") %>% html_text()
intensity = page %>% html_nodes("#cf_hagn~ .paws .paws .child-characteristic:nth-child(3) .characteristic-star-block") %>% html_text()
exercise_needs = page %>% html_nodes("#cf_hagn~ .paws .paws .child-characteristic:nth-child(4) .characteristic-star-block") %>% html_text()
playfulness = page %>% html_nodes("#cf_hagn~ .paws .paws .child-characteristic:nth-child(5) .characteristic-star-block") %>% html_text()
breed_group = page %>% html_nodes(".vital-stat-box:nth-child(1)") %>% html_text()
height = page %>% html_nodes(".vital-stat-box:nth-child(2)") %>% html_text()
weight = page %>% html_nodes(".vital-stat-box:nth-child(3)") %>% html_text()
life_span = page %>% html_nodes(".vital-stat-box:nth-child(4)") %>% html_text()
# Create a data frame
dogs = data.frame(breed, adaptability, apartment_living, novice_owners, sensitivity_level, tolerates_alone, tolerates_cold, tolerates_hot, friendliness, affectionate, kid_friendly, dog_friendly, stranger_friendly, health_grooming, shedding, drooling, easy_groom, general_health, weight_gain, size, trainability, easy_train, intelligence, mouthiness, prey_drive, barking, wanderlust, physical_needs, energy_level, intensity, exercise_needs, playfulness, breed_group, height, weight, life_span, stringsAsFactors = FALSE)
# view data frame
View(dogs)
抱歉,代碼中有很多變數要存盤。我想我將需要使用 for 回圈來遍歷各個品種的每個不同的 url,但我不確定我會如何寫,因為“i”值是字符而不是數字。
任何人都可以建議這是否是最好的方法,如果是,我將如何實作這一目標?
非常感謝您的幫助,
詹姆士
uj5u.com熱心網友回復:
我們可以使用html_attr('href')來獲取所有犬種的鏈接,
library(rvest)
library(dplyr)
url = url = 'https://dogtime.com/dog-breeds/profiles'
url %>% read_html() %>%
html_nodes(".list-item-img") %>%
html_attr('href')
輸出
[1] "https://dogtime.com/dog-breeds/afador"
[2] "https://dogtime.com/dog-breeds/affenhuahua"
[3] "https://dogtime.com/dog-breeds/affenpinscher"
[4] "https://dogtime.com/dog-breeds/afghan-hound"
[5] "https://dogtime.com/dog-breeds/airedale-terrier"
您可以使用代碼遍歷鏈接。
此外,我建議您使用class來獲取資料,因為它將大塊代碼減少到小塊,
url = "https://dogtime.com/dog-breeds/golden-retriever"
url %>% read_html() %>%
html_nodes(".characteristic-title") %>%
html_text()
[1] " Adaptability" "Adapts Well To Apartment Living" "Good For Novice Owners"
[4] "Sensitivity Level" "Tolerates Being Alone" "Tolerates Cold Weather"
[7] "Tolerates Hot Weather" " All Around Friendliness" "Affectionate With Family"
[10] "Kid-Friendly" "Dog Friendly" "Friendly Toward Strangers"
[13] " Health And Grooming Needs" "Amount Of Shedding" "Drooling Potential"
[16] "Easy To Groom" "General Health" "Potential For Weight Gain"
[19] "Size" " Trainability" "Easy To Train"
[22] "Intelligence" "Potential For Mouthiness" "Prey Drive"
[25] "Tendency To Bark Or Howl" "Wanderlust Potential" " Physical Needs"
[28] "Energy Level" "Intensity" "Exercise Needs"
[31] "Potential For Playfulness"
url %>% read_html() %>%
html_nodes(".characteristic-star-block") %>% html_nodes('.star') %>%
html_text()
[1] "" "2" "3" "5" "1" "3" "3" "" "5" "5" "5" "5" "" "5" "4" "2" "2" "5" "3" "" "5" "5" "5" "3" "3" "2" "" "5" "2" "5" "5"
uj5u.com熱心網友回復:
如果您對金毛獵犬的代碼感到滿意,這將為您提供所有狗的字符向量,
link = "https://dogtime.com/dog-breeds/"
dogs <- page %>% html_nodes(".list-item") %>% html_text()
然后你可以paste如下,
dog_urls <- paste0(link, dogs)
并在回圈中使用您現有的代碼來恢復所有的狗。
dog_data <- list()
for dog_url in dog_urls {
dog_data <- append(dog_data, scrape_function(dog_url)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/390852.html
