html - 如何排除 R 中的某些节点?
问题描述
我正在抓取一个网站并想排除一些节点。
url <- "https://www.n11.com/telefon-ve-aksesuarlari/cep-telefonu?q=iphone+11"
gettitles <- read_html(url) %>%
html_nodes("div.productArea") %>%
html_nodes(":not(div.group.listingGroup.set6.promoGroup)") %>%
html_nodes(xpath = '//*[@class="plink"]') %>%
html_text() %>%
tibble()
我不希望分页下页面底部的标题。但它不起作用。它应该是 28,但 42 中的 14 是突出的。这段代码有什么问题?谢谢。
解决方案
很少有属性可以区分页面底部和主要节点中的标题。我们可以使用这些属性之一并过滤节点。我"data-ctgid"
这里用过。
library(rvest)
url <- "https://www.n11.com/telefon-ve-aksesuarlari/cep-telefonu?q=iphone+11"
nodes <- read_html(url) %>% html_nodes("div.columnContent")
nodes[!is.na(nodes %>% html_attr('data-ctgid'))] %>%
html_nodes('div.pro a') %>%
html_attr('title')
#[1] "iPHONE 11 128 GB APPLE TÜRKİYE GARANTİLİ"
#[2] "APPLE İPHONE 11 64 GB (APPLE TÜRKİYE GARANTİLİ)"
#[3] "Apple iPhone 11 128GB (2 Yıl Apple Türkiye Garantili)"
#[4] "Apple iPhone 11 Pro Max 64 GB (2 Yıl Apple Türkiye Garantili)"
#[5] "Apple iPhone 11 Pro 64 GB (2 Yıl Apple Türkiye Garantili)"
#[6] "APPLE İPHONE 11 64 GB (2 YIL APPLE TÜRKİYE GARANTİLİ)"
#...
#...
#[27] "iPhone 11 Pro 64 GB"
#[28] "Apple iPhone 11 64 GB (Distribütör Garantili)"
推荐阅读
- php - 使用 MYSQL 和 PHP 查询以逗号分隔的字符串中的项目返回完全匹配
- python - 为 request.get 创建参数时的错误
- java - “String @NotNull []”语法导致 intellij IDEA 插件中的“'@NotNull' 不适用于类型使用”
- mongodb - 如何只从 Mongodb 聚合中返回一些字段($project),同时还使用 $match、$lookup 和 $filter
- javascript - Nestjs / Jestjs:收到:未定义,预期:1
- java - 鉴于检查的错误不会在我的程序中抛出,这个未经检查的包装器是否可以接受
- javascript - 数组减少累加器为空数组,推送不起作用
- python - Pyexcelerate:设置表格样式格式
- html - 如何删除输入的边框
- python - 如何优化查询集过滤