0

Hi people!

Im having an issue, and it is really bothering me.

I want to get some content (only 2 lines) from another site and paste it into my own site. The problem is that the content on the other site keeps changing, and I need it to be auto updated on my site.

How is this possible, to scrape theese few lines from another site with PHP?

Thanks in advance.

4
Contributors
11
Replies
13
Views
6 Years
Discussion Span
Last Post by smantscheff
0

hi,
well, how frequently does the content change?

Just a thought ..... may be u can set the approximate time interval for the page refresh using meta tag or an ajax request to refresh the page every X seconds.

0

I know that I can make a page refresh, but that doesnt get me anywhere if I dont have imported the content from the other site to my own. How do I import that?

It's not my own site I want the content from, it's from another site (maybe located in US? I dont know).. I heard that I could do a page scrape, but how?

0

You have to first get the contents of that page. Some options for this would be cURL, file_get_contents, sockets, fopen/fread/fclose, etc.

Once you have the content you have a few options for extracting that particular piece of information. You can try to use a regular expression with the preg functions to match on the tags around it. You can try loading it into simpleXML and using xpath to select the node from the document, drawback to this method is simpleXML does not like invalid documents (most html).

Or, my personal favorite use the php DOM object, set it to load HTML so it only throws warnings about invalid markup, suppress or trap the warnings so they're not displayed and then use xpath to grab just the nodes that contain the content you want.

Once you have your scraped data cache it so you don't need to make the request on every single page load and you'll have a pretty effective solution.

0

So it would be something like this? $html = file_get_html('http://www.google.com/'); But how do I use xpath (which I have never heard of) to get the specific data from that site?

0

In this example I load the html from the daniweb homepage and than query on ALL div's with id='stats', which should only be one. See the link below for some good tutorials on crash course xpath.

<?php
$html = file_get_contents('http://www.daniweb.com');

$dom = new DOMDocument('1.0', 'iso-8859-1');

//Suppress any warnings from invalid html markup
@$dom->loadHTML( $html );
$xpath = new DOMXPath( $dom );
$query = '//div[@id="stats"]';

$nodes = $xpath->query( $query );
foreach( $nodes as $node ){
	echo $node->nodeValue;
}

XPath: http://www.w3schools.com/xpath/default.asp
DomDocument: http://www.php.net/manual/en/class.domdocument.php
DomXpath: http://www.php.net/manual/en/class.domxpath.php

0

Ah okay, I want to read more about it thanks! BUT, I think there is a problem.. The content I want to grab is not inside a div, but a table.

This is the lines I want:

<script type="text/javascript">
              var flashvars = {
                'feedback_url':'http://site.com/test.php?args=558065,31,1298047277:11xM1GGRWCA/trailer.flv',
                'file': '1298047277:11xM1GGRWCA/trailer.flv',
                                              };
			</script>

feedback_url and file, as you can see above, is the ones I want.

0

You can navigate to any node anywhere in an xml based (html) document with XPath. But, the path to get to that script node depends entirely on the rest of the document it is contained within.

For example if it was the first script tag in the head: /html/head/script[1] Once you have your selection narrowed down to that node you can easily parse the resulting content for those values specifically.

Edited by mschroeder: n/a

0

Thanks for your help, but I dont seem to get it. But I guess I now know how difficult it would be.

Fx. this is not working, not displaying anything:

<?php
$html = file_get_contents('http://www.daniweb.com');

$dom = new DOMDocument('1.0', 'iso-8859-1');

//Suppress any warnings from invalid html markup
@$dom->loadHTML( $html );
$xpath = new DOMXPath( $dom );
$query = '//html/head/script[1]';

$nodes = $xpath->query( $query );
foreach( $nodes as $node ){
	echo $node->nodeValue;
}
?>
0

You could also use a regular expression to extract the desired content:

if (preg_match( '~feedback_url\':\s*\'([^\']+)\',\s\'file\':\s*([^\']+)'~, $sourcecode, $match )) {
  $feedback_url = $match[1];
  $file = $match[2];
}

Edited by smantscheff: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.