0

I want to scrape through some product link (about 80) from a page to obtain the price value of each item.

But some items (may be 10-15% of all) are missing their price value.

My code is below:

link=page %>% html_nodes(".product-item-link") %>% html_attr("href") 

get_price=function(link){
  item_page=read_html(link)
  item_price=item_page %>% html_nodes(".price") %>% 
    html_text() 
  return(item_price)
}

price=sapply(link,FUN=get_price,USE.NAMES=FALSE)

May I ask what code should I write/ revise if I want to show a text of "No Price Info." as a result when I loop scraping, instead of error?

Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
S C
  • 1
  • 2
    Your code doesn't return an error when I run it on a web page without any nodes of class "product-item-link" at all. Did you mean "empty" instead of "error" perhaps? Otherwise please include the error message (and the URL you're trying to scrape). – I_O Jun 10 '23 at 06:22
  • Could you share the link so we can debug your code better? – Chamkrai Jun 10 '23 at 07:44
  • 1
    See this question/answer: https://stackoverflow.com/questions/56673908/how-do-you-scrape-items-together-so-you-dont-lose-the-index/56675147#56675147 – Dave2e Jun 10 '23 at 13:29
  • By *"missing their price value"* , do you mean that some of the links in link list are dead / non-existing and it's `read_html()` in the function that results in an error? In that case `purrr::possibly()` can be handy, always returns a value instead of an error. Some examples with `rvest` -- https://stackoverflow.com/search?q=%5Brvest%5D+possibly – margusl Jun 11 '23 at 11:50

0 Answers0