How to use Xpath in PHP Simple HTML DOM Parser

后端 未结 2 850
南旧
南旧 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: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);
    

提交回复
热议问题