rvest - scrape 2 classes in 1 tag

前端 未结 1 1055
臣服心动
臣服心动 2021-02-14 17:35

I am new to rvest. How do I extract those elements with 2 class names or only 1 class name in tag?

This is my code and issue:

doc <- paste(\"

        
1条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-14 18:33

    You can use css selector as follows:

    Select class contains b1 not a1:

    read_html(doc) %>% html_nodes(".b1:not(.a1)")
    # {xml_nodeset (1)}
    # [1]  text2 
    

    Or use the attribute selector:

    read_html(doc) %>% html_nodes("[class='b1']")
    # {xml_nodeset (1)}
    # [1]  text2 
    

    Select class contains both:

    read_html(doc) %>% html_nodes(".a1.b1")
    # {xml_nodeset (1)}
    # [1]  text1 
    

    0 讨论(0)
提交回复
热议问题