rvest - scrape 2 classes in 1 tag

前端 未结 1 1735
清酒与你
清酒与你 2021-02-14 17:45

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:08

    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] <span class="b1"> text2 </span>
    

    Or use the attribute selector:

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

    Select class contains both:

    read_html(doc) %>% html_nodes(".a1.b1")
    # {xml_nodeset (1)}
    # [1] <span class="a1 b1"> text1 </span>
    
    0 讨论(0)
提交回复
热议问题