【问题标题】:R web scraping issue on extracting content through rvest通过 rvest 提取内容的 R web 抓取问题
【发布时间】:2020-09-29 18:50:15
【问题描述】:

我正在尝试从https://careers.microsoft.com/us/en/search-results 中提取内容并从页面中获取标题、信息等

urlString <- "https://careers.microsoft.com/us/en/search-results?"
getHTML <- xml2::read_html(urlString)
t2 <- getHTML %>% html_text() %>% stringr::str_sub(start = 8027, end = 65679)
jsonWeb <- jsonlite::fromJSON(t2)
df <- jsonWeb$data$jobs

有没有更优雅的方法呢?就像提取 phApp.ddo {} 的 json 非常感谢

【问题讨论】:

    标签: r json web-scraping rvest


    【解决方案1】:

    这样的网站抓取无法获得可靠的结果,因为您无法控制抓取的内容。但是,通过子字符串索引来做是一场灾难,因为几乎任何动态内容的变化都会破坏你的代码(事实上,你的代码对我不起作用,因为我提供的 json 字符串略短,所以我得到了尾随不会解析的垃圾)。

    一个更强大的解决方案(尽管请参阅下面的警告)是在 json 字符串的开头和结尾找到有用的分隔符,您可以使用它来删除您不需要的部分。

    urlString <- "https://careers.microsoft.com/us/en/search-results?"
    getHTML <- xml2::read_html(urlString)
    
    json <- jsonlite::fromJSON(strsplit(strsplit(html_text(getHTML), 
            "phApp\\.ddo = ")[[1]][2], "; phApp")[[1]][1])
      
    json$eagerLoadRefineSearch$data$jobs
    
    #> # A tibble: 50 x 27
    #>    country subCategory industry title multi_location type  orgFunction
    #>    <chr>   <chr>       <lgl>    <chr> <list>         <lgl> <lgl>      
    #>  1 United~ Software E~ NA       Prin~ <chr [1]>      NA    NA         
    #>  2 United~ Art         NA       Lead~ <chr [1]>      NA    NA         
    #>  3 India   Support En~ NA       Supp~ <chr [1]>      NA    NA         
    #>  4 Romania Support En~ NA       Micr~ <chr [2]>      NA    NA         
    #>  5 China   Solution S~ NA       Seni~ <chr [1]>      NA    NA         
    #>  6 United~ Software E~ NA       Soft~ <chr [1]>      NA    NA         
    #>  7 India   Support En~ NA       Supp~ <chr [1]>      NA    NA         
    #>  8 United~ Software E~ NA       Seni~ <chr [1]>      NA    NA         
    #>  9 Japan   Marketing ~ NA       Full~ <chr [1]>      NA    NA         
    #> 10 United~ Software E~ NA       Seni~ <chr [1]>      NA    NA         
    #> # ... with 40 more rows, and 20 more variables: experience <chr>,
    #> #   locale <chr>, multi_location_array <list>, jobSeqNo <chr>,
    #> #   postedDate <chr>, searchresults_display <lgl>,
    #> #   descriptionTeaser <chr>, dateCreated <chr>, state <chr>,
    #> #   targetLevel <chr>, jd_display <lgl>, reqId <lgl>, badge <chr>,
    #> #   jobId <chr>, isMultiLocation <lgl>, jobVisibility <list>,
    #> #   mostpopular <dbl>, location <chr>, category <chr>,
    #> #   locationLatlong <lgl>
    

    我同意如果你可以只请求 json 会更好,但在这种情况下,页面是在服务器端构建的,所以没有对提供 json 的 API 的独立 xhr 请求,所以你需要雕刻 json在提供的 HTML 之外。正则表达式对此并不理想,但它比剪断固定长度的字符串要好得多。

    【讨论】:

      【解决方案2】:

      使用V8包在页面上运行JS脚本获取phApp对象:

      library(rvest)
      library(V8)
      pg <- read_html("https://careers.microsoft.com/us/en/search-results")
      scripts <- pg %>% html_nodes(xpath = "//script[contains(.,'phApp')]") %>% html_text()
      ct <- v8()
      ct$eval("var phApp = {}")
      for (js in scripts) ct$eval(js)
      data <- ct$get("phApp")
      jobs <- data$ddo$eagerLoadRefineSearch$data$jobs
      

      【讨论】:

      • 感谢您提供有关 V8 软件包的信息....我也在尝试。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      相关资源
      最近更新 更多