How to use Xpath in PHP Simple HTML DOM Parser

后端 未结 2 829
南旧
南旧 2021-01-07 09:32

I am learning scraping using the PHP Simple HTML DOM Parser and Xpath. Accroding to the changelog given here http://sourceforge.net/news/?group_id=218559. The PHP SImple HTM

相关标签:
2条回答
  • 2021-01-07 10:31

    The find function. http://simplehtmldom.sourceforge.net/manual.htm#section_find

    $content = file_get_html($link);
    
    $elems = $content->find("/html/body/div/div");
    
    0 讨论(0)
  • 2021-01-07 10:38

    There's find function in Simple HTML DOM Parser. BUT it accepts selectors! So you cant just write

    $html = file_get_html($link);
    $elements = $html->find("/html/body/div/p");
    

    because then you'll get more than one element!

    to get just exact element marked by XPath, write

    $element = $html->find("/html/body/div[1]/p[1]", 0);
    
    0 讨论(0)
提交回复
热议问题