R How to Check if XPath Exists

前端 未结 2 1229
旧巷少年郎
旧巷少年郎 2021-01-24 17:17

hoping someone more knowledgeable than me can throw some light here.

As part of a larger web-scraper I want to pull meta data out of a set of pages. When I ran this it

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-24 17:39

    Assuming the error comes when you try and process the empty list...

    > parsed <- htmlParse("http://www.coindesk.com/information")
    > meta <- xpathApply(parsed, "//meta[starts-with(@property, \"og:description\")]", xmlGetAttr,"content")
    > meta
    list()
    > length(meta)==0
    [1] TRUE
    

    Then test for length(meta)==0 - which is TRUE if the element is missing. Otherwise its FALSE - as in this example of extracting the title property:

    > meta <- xpathApply(parsed, "//meta[starts-with(@property, \"og:title\")]", xmlGetAttr,"content")
    > meta
    [[1]]
    [1] "Beginner's guide to bitcoin - CoinDesk's Information Center"
    
    > length(meta)==0
    [1] FALSE
    

提交回复
热议问题