Retrieving specific data from a web page using PHP

Possible duplicate:
HTML Screening in Php

I would like to know if there is a way to get a specific line of text from a web page that is updated every time using PHP. I searched "all over the Internet" and found nothing. I just saw that preg_match can do this, but I did not understand how to use it.

Imagine the web page contains the following:

<div name="changeable_text">**GET THIS TEXT**</div> 

How can I do this with PHP, after using file_get_contents to put the page in a variable?

Thanks in advance:)

+6
source share
4 answers

You can use DOMDocument , for example:

 $html = file_get_contents( $url); libxml_use_internal_errors( true); $doc = new DOMDocument; $doc->loadHTML( $html); $xpath = new DOMXpath( $doc); // A name attribute on a <div>??? $node = $xpath->query( '//div[@name="changeable_text"]')->item( 0); echo $node->textContent; // This will print **GET THIS TEXT** 
+10
source

Maybe you should take a look at

Simple HTML DOM Library

There is a small tutorial here: http://www.developertutorials.com/tutorials/php/easy-screen-scraping-in-php-simple-html-dom-library-simplehtmldom-398/

This is one of the screenshot screenshot APIs that allows you to load html and then get parts of it in a similar jQuery language.

+2
source

You say data cleansing : the act of retrieving data from user readable output. In your case, this is all that is between the <div> tags. Use the PHP DOM extension to get the tag you want and extract the data. Google is looking for a tutorial on the PHP DOM.

0
source
 $delements= file_get_html('url will go here'); foreach($elements->find('element') as $ele) { //traverse according to your preferences } //return or output 
0
source

Source: https://habr.com/ru/post/920913/


All Articles