簡體   English   中英

為什么我的 SelectorGadget 在使用 R 進行 Webscraping 時不會 select 某些節點

[英]Why does my SelectorGadget does not select certain nodes while Webscraping with R

因此,我在 R 中編寫了以下代碼,用於在給定站點中進行網絡抓取 - 而在其他站點中,此代碼有效(當然在選擇所需節點之后),這里它不返回任何內容。 似乎網站本身在最初打開它時不允許 select 這樣的事情,即使使用鼠標指針也是如此。 所以我想知道如何繞過這個(因為它也發生在其他一些網站上)。

install.packages('rvest')
install.packages('stringr')
install.packages('magrittr')
install.packages('tidyverse')
library(rvest)
library(stringr)
library(magrittr)
library(tidyverse)

#Pirmais
url_base <- "https://alkoutlet.lv/dzerieni/stiprie/rums.html?page="
l_out <- 2
urls <- paste0(url_base, seq(1, by = 1, length.out = l_out))
urls
# Helper function for parsing overview


parse_overview <- function(x){
  tibble(
    title = html_text(html_nodes(x, '.ProductCard-Name_isLoaded'), TRUE),
    price = html_text(html_nodes(x, '.ProductCard-PriceWrapper'), TRUE),
    description = html_text(html_nodes(x, '.ProductCard-ShortSpecification'), TRUE),
    link = str_trim(html_attr(html_nodes(x, '.ProductCard-Name_isLoaded'), 'href'))%>%paste("https://alkoutlet.lv",.,sep=""))
}

# Scrape overview    
Result <- urls %>% 
  map(read_html) %>% 
  map_df(parse_overview)

View(Result)

這是網站中關於朗姆酒部分的前 2 頁 - 我正在嘗試抓取價格、描述和名稱(還有鏈接,但我不確定我是否選擇了正確的節點)。

有誰知道如何使它工作? 當您第一次打開它時,該站點似乎沒有向 select 提供節點,因此這可能是某種預防措施 - 那么如何繞過它呢?

該信息未加載為 HTML。 我建議使用這種方法:

library(tidyverse)
library(httr2)

'https://alkoutlet.lv/graphql?hash=2951167027&sort_1={"name":"ASC"}&filter_1={"price":{},"category_id":{"eq":13},"customer_group_id":{"eq":"0"}}&pageSize_1=24&currentPage_1=2&_currency=""' %>% 
  request() %>%  
  req_perform() %>%  
  resp_body_json(simplifyVector = TRUE) %>% 
  .$data %>% 
  .$products %>% 
  .$items %>% 
  as_tibble()

# A tibble: 24 x 23
      id sku    name     type_id stock~1 volum~2 alco_~3 sugge~4 categ~5 goodW~6 price~7 thumb~8
   <int> <chr>  <chr>    <chr>   <chr>     <int>   <int> <chr>   <list>  <lgl>     <dbl> <chr>  
 1   642 366419 Rums Co~ simple  IN_STO~      82      83 NA      <df>    NA         1.76 /imp/o~
 2   634 366433 Rums Co~ simple  IN_STO~      82      83 NA      <df>    NA         1.26 /imp/o~
 3   631 366443 Rums Co~ simple  IN_STO~      82      83 NA      <df>    NA         1.26 /imp/o~
 4   672 366310 Rums Co~ simple  IN_STO~      82     813 NA      <df>    NA         1    /imp/o~
 5  3148 366584 Rums Da~ simple  IN_STO~      82     835 NA      <df>    NA         4    /3/6/3~
 6  3147 366589 Rums Da~ simple  IN_STO~      82     835 NA      <df>    NA         4    /3/6/3~
 7  2644 364746 Rums De~ simple  IN_STO~      82     835 672     <df>    NA         3    /3/6/3~
 8  2595 366565 Rums De~ simple  IN_STO~      82     894 672     <df>    NA         3    /3/6/3~
 9  2896 364747 Rums De~ simple  IN_STO~      82     835 672     <df>    NA         3    /3/6/3~
10   810 362939 Rums De~ simple  IN_STO~      82      83 NA      <df>    NA         4    /3/6/3~
# ... with 14 more rows, 17 more variables:
#   price_range$minimum_price$discount$percent_off <dbl>,
#   price_range$minimum_price$final_price <df[,2]>, $$final_price_excl_tax <df[,2]>,
#   $$regular_price <df[,2]>, $$regular_price_excl_tax <df[,2]>, thumbnail$url <chr>,
#   small_image <df[,2]>, short_description <df[,1]>, stock_item <df[,2]>,
#   special_from_date <chr>, special_to_date <chr>, price_tiers <list>, attributes <list>,
#   url <chr>, review_count <int>, rating_summary <int>, mp_label_data <list>, and ...
# i Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM