R 如何提取div中第二个p元素中的文本

R 如何提取div中第二个p元素中的文本,r,web-scraping,rvest,rselenium,R,Web Scraping,Rvest,Rselenium,我有一个有两个p标签的div 我需要得到第二个p元素的文本 <div class="fb-price-list"> <p class="fb-price">S/ 1,699 (Internet)</p> <p class="fb-price">S/ 2,399 (Normal)</p> </div> 我有这个,但不起作用: tvs_url <- read_html("https://www

我有一个有两个p标签的div

我需要得到第二个p元素的文本

<div class="fb-price-list">
      <p class="fb-price">S/  1,699 (Internet)</p>
      <p class="fb-price">S/  2,399 (Normal)</p>
</div>
我有这个,但不起作用:

tvs_url <- read_html("https://www.falabella.com.pe/falabella-pe/category/cat210477/TV-Televisores?page=1")

product_price_actual <- tvs_url %>% 
  html_nodes('div.pod-group pod-group__large-pod div.pod-body div.fb-price-list p.fb-price:nth-child(2)') %>%
  html_text()
html:


比较29%AGREGAR A TU BOLSA这里我使用css选择具有类别fb价目表的节点,然后选择第二个p子节点:

tl;博士

内容是动态加载的,但可以作为字符串使用,源代码是javascript字典,可以在正则表达式之后使用json解析器对其进行解析以获取字符串。是当前提取的json

如果您使用F12打开开发工具并检查页面html,您将看到包含javascript字典的脚本标记,该字典可以通过json解析器提取和处理。这意味着您可以将显示的脚本标记作为目标,然后从节点和子字符串中提取文本,但我更喜欢字符串上的正则表达式,请参见我将主体提取为字符串。正则表达式通常不建议用于HTML,但可以用于字符串

代码输出:

提供包含数据帧的长度为32的列表。您可以看到,在每个dataframe originalPice中包含您想要的信息,其中label column==Normal

并非每件商品都有原价。以下是一种简单但不一定是最有效的写出值的方法:

l <- json$state$searchItemList$resultList$prices

for (i in l){
  if (length(i$originalPrice)>1){
    print(i$originalPrice[2])
  } else {
    print("No original price")
  }
}
R

正则表达式解释:


似乎是动态的,因此数据来自其他地方。我用JSON、XML等数据查找GET响应,但没有找到任何结果。在这一点上我会选择硒。下面应该提取正确的节点。可以使用任何方法从结果字符串中提取数字:

安装.packagesRSelenium 硒库 图书馆服务 驱动程序

因为你也认为RSE是一个对应的包的解决方案。 您可以找到元素,例如通过xpath。在您的例子中,xpath是:/html/body/div/main/div/div/div/section/div/div/div/div/div/a/div/p

它类似于@gersht的解决方案,但仅使用硒

可复制示例:

结果/产出:

设置:

如果需要,请参阅此处关于如何设置RSenelium:

编辑:

在注释中的注释之后,我也会捕获空元素,我会得到父元素,然后处理价格文本


父元素是/html/body/div/main/div/div/section/div/div/div/div/div/a/div[@class='fb price list']如果其中一个价格不可用,则包含一个空字符串。

在您的实际URL上是否有任何答案对您有效,因为当我将其应用于https://www.falabella.com.pe/falabella-pe/category/cat210477/TV-Televisores?page=1 %>%阅读\u html%>%html\u nodes.fb-price-list p:nth-child2%>%html\u我得到的文本character0@RonakShah你是正当它在url中不起作用,只是在我处理的html部分起作用。奇怪的请告诉我您是否可以帮我解决这个问题。您是否可以使用RSelenium?@BigDataScientist是的,我会使用RSelenium,您如何为第二个p标记指定xpath?在提供的xpath中,我只看到1p标记。我需要从两个p标签中获取文本,在我在答案中提供的html中,并使用元素创建一个数据框,同时考虑到有时任何p标签中都不可能有文本,在这种情况下,我需要一个NA来填充该列的行。请参阅上面的编辑,。。如果您想捕获缺少的价格,我会选择parent element.ty,根据您的回答,我已经做了我需要的。您可能会看到我的更新,以在没有精确性时进行检查。我有类似的p.fb-price:nth-child2,您只能使用。fb-price:nth-child2,我是否应该针对标签和类?ty。非常有趣的发现和解决方案。显然,他们使用react作为前端,并将数据作为json发送到place。我需要进一步调查一下,因为我看到我也可以用这个来获得产品名称和品牌。这很容易。关键是名称的标题和品牌的品牌
library(rvest)

"<div class=\"pod-item\"><div class=\"fb-form__input--checkbox fb-pod__item__compare\"><input id=\"fb-pod__item__input-16754140\" class=\"fb-pod__item__compare__input\" type=\"checkbox\" name=\"fb-pod__item__input-16754140\" value=\"16754140\"><label for=\"fb-pod__item__input-16754140\" class=\"fb-pod__item__compare__label\">Comparar</label></div><div class=\"pod-head\"><a class=\"pod-head__image\" href=\"/falabella-pe/product/16754140/LED-UHD-4K-55-Smart-TV-UN55RU7100GXPE-SERIE-RU7100/16754140\"><div class=\"content__image\"><img src=\"//falabella.scene7.com/is/image/FalabellaPE/16754140?wid=544&amp;hei=544&amp;qlt=70&amp;anchor=750,750&amp;crop=0,0,0,0\" alt=\"img\" class=\"image\"></div></a><a href=\"/falabella-pe/product/16754140/LED-UHD-4K-55-Smart-TV-UN55RU7100GXPE-SERIE-RU7100/16754140\" class=\"pod-head__stickerslink\"><div class=\"pod-head__stickers\"><div class=\"fb-responsive-flag fb-responsive-stylised-caps fb-pod__flag fb-pod__flag--percentoff\" data-discount-content=\"\">29%</div></div></a></div><div class=\"pod-body\"><a class=\"section__pod-top\" href=\"/falabella-pe/product/16754140/LED-UHD-4K-55-Smart-TV-UN55RU7100GXPE-SERIE-RU7100/16754140\"><div class=\"section__pod-top-brand\">SAMSUNG</div><div class=\"section__pod-top-title\"><div class=\"LinesEllipsis  \">LED UHD 4K 55\" Smart TV UN55RU7100GXPE SERIE RU7100<wbr></div></div></a><div class=\"section__pod-middle\"><div class=\"section__pod-middle-content__stickers\"><div class=\"fb-responsive-flag fb-responsive-stylised-caps fb-pod__flag fb-pod__flag--percentoff\" data-discount-content=\"\">29%</div></div><div class=\"section__information\"><a class=\"section__information-link\" href=\"/falabella-pe/product/16754140/LED-UHD-4K-55-Smart-TV-UN55RU7100GXPE-SERIE-RU7100/16754140\"><div class=\"fb-price-list\"><p class=\"fb-price\">S/  1,699 (Internet)</p><p class=\"fb-price\">S/  2,399 (Normal)</p></div></a></div><div class=\"section__pod-middle-content__button\"><button class=\"btn-add-to-basket\">AGREGAR A TU BOLSA</button></div></div><div class=\"section__pod-bottom\"><div class=\"fb-pod__rating\" style=\"visibility: hidden;\"><a href=\"/falabella-pe/product/16754140/LED-UHD-4K-55-Smart-TV-UN55RU7100GXPE-SERIE-RU7100/16754140#comments\"><div class=\"fb-rating-stars\"><div class=\"fb-rating-stars__container\"><div class=\"fb-rating-stars__holder\"><span class=\"\"><i class=\"icon-rating\"></i></span></div><div class=\"fb-rating-stars__holder\"><span class=\"\"><i class=\"icon-rating\"></i></span></div><div class=\"fb-rating-stars__holder\"><span class=\"\"><i class=\"icon-rating\"></i></span></div><div class=\"fb-rating-stars__holder\"><span class=\"\"><i class=\"icon-rating\"></i></span></div><div class=\"fb-rating-stars__holder\"><span class=\"\"><i class=\"icon-rating\"></i></span></div><p class=\"fb-rating-stars__count\">0 <span class=\"fb-rating-stars__count__max\"> / 5</span></p></div></div></a></div><a class=\"section__pod-bottom-descriptionlink\" href=\"/falabella-pe/product/16754140/LED-UHD-4K-55-Smart-TV-UN55RU7100GXPE-SERIE-RU7100/16754140\"><ul class=\"section__pod-bottom-description\"><li>Modelo: UN55RU7100GXPE</li><li>Tamaño de la pantalla: 55\"</li><li>Resolución: 4K Ultra HD</li><li>Tecnología: Led</li><li>Conexión bluetooth: Sí</li></ul></a></div></div></div>" %>% 
  read_html() %>% 
  html_nodes(".fb-price-list p:nth-child(2)") %>% 
  html_text()
json$state$searchItemList$resultList$prices
l <- json$state$searchItemList$resultList$prices

for (i in l){
  if (length(i$originalPrice)>1){
    print(i$originalPrice[2])
  } else {
    print("No original price")
  }
}
library(rvest)
library(jsonlite)
library(stringr)

url = 'https://www.falabella.com.pe/falabella-pe/category/cat210477/TV-Televisores?page=1'
r <- read_html(url) %>%
  html_node('body') %>%
  html_text() %>%
  toString()
x <- str_match_all(r,'fbra_browseProductListConfig = (.*);')
json <- jsonlite::fromJSON(x[[1]][,2])
print(json$state$searchItemList$resultList$prices)
library(RSelenium)

rD <- rsDriver() 
remDr <- rD$client

remDr$navigate(url)
priceElems = remDr$findElements(
  using = "xpath", 
  value = "/html/body/div/main/div/div/div/section/div/div/div/div/div/a/div[@class = 'fb-price-list']"
)

rawPrices = sapply(
  X = priceElems, 
  FUN = function(elem) elem$getElementText()
)

splitted = sapply(
  X = rawPrices, 
  FUN = strsplit, 
  split = "\nS/"
)

prices = data.frame(
  internetPrices = sapply(splitted, "[", 1),
  normalPrices = sapply(splitted, "[", 2)
)
> head(prices, 8)
       internetPrices    normalPrices
1 S/ 1,099 (Internet)  1,599 (Normal)
2 S/ 2,299 (Internet)  3,999 (Normal)
3 S/ 1,699 (Internet)  2,399 (Normal)
4   S/ 999 (Internet)  1,149 (Normal)
5   S/ 999 (Internet)  1,399 (Normal)
6 S/ 1,399 (Internet)  1,699 (Normal)
7 S/ 2,199 (Internet)            <NA>
8 S/ 2,699 (Internet)  4,999 (Normal)