PHP Simple HTML DOM Parser works without id or class reference?

Asked

Viewed 688 times

1

I want to use the PHP Simple HTML DOM Parser to remove certain links from a page generated in Frontpage the problem I found is the following as Frontpage does not generate any html element with id or class i wonder if it is possible to indicate a way for this to be done

the page on which I want to remove the links is not mine, I’m just extracting data from it

1 answer

1


Yes. You must use a property called XPath, indicating the path of an element in the DOM (Document Object model).

To catch the XPath of an element, simply access the Elements tab of Developer Tools (in the case of Google Chrome), inspect the element in question and then copy your Xpath:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

In the case of the link above, for example, its Xpath is:

//*[@id="supertopo"]/div[1]/div[1]/ul/li[1]/a

I hope I’ve helped. :)

  • my returned Xpath was the following /html/body/table/tbody/tr[1]/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr[3]/td/table/tbody/tr[1]/td/div font/p[1]//Strong Array to string notice

  • It is because the method probably returns an array of nodes, even if it has only one node. You should iterate through this array.

  • sorry but did not understand! has some example?

  • Which line of code returns "Array to string Conversion"?

  • include('simple_html_dom.php');
$content = file_get_html('http://www.abresi.com.br/noticia_700.htm');
$elems = $content->find("/html/body/table/tbody/tr[1]/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr[3]/td/table/tbody/tr[1]/td/div/p[1]/font/strong");
echo $elems; on line 5

  • @Guilhermehenrique see his documentation: http://simplehtmldom.sourceforge.net/. The method find returns an array, so you need to scroll through the elements or go straight to the index of the element with find("/html/body/table/tbody/tr[1]/td[2]/table/tbody/tr[2]/td/table/tb‌​ody/tr/td[2]/table/tbody/tr[3]/td/table/tbody/tr[1]/td/div/p[1]/font/strong", 0). See if it works.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.