Get Div Content from External Website

Get DIV content from external Website

This is what I always use:

$url = 'https://somedomain.com/somesite/';
$content = file_get_contents($url);
$first_step = explode( '<div id="thediv">' , $content );
$second_step = explode("</div>" , $first_step[1] );

echo $second_step[0];

Get/load dynamic DIV content from an external website

Please see @hakre 's comment above. It provided the answer to my question.

A little background on how:
It tuned out that my original idea of parsing the whole page and looking for the content of a particular div could be simplified. I loaded the public service website in question in my browser and opened the developer toolkit (F12); navigated to the tab "Network", reloaded the page and applied the filters to only sniff JS and XHR traffic. From there I was able to extract the endpoint URL responsible for dynamically loading the content of the <div class="last-license-plate-truck"></div> div. It turned out the endpoint (REST API) responded with a nice JSON format which I could easily grab using PHP.

php get all div content from external website

Try
PHP Simple HTML DOM Parser

Get specific content from an external url - PHP

Sure, it can be done easily with php's file_get_contents, DOMDocument and DOMXPath.

The following example gets the HREF value from the 'Stack Overflow' logo's <a> tag, which has the class -logo js-gps-track:

$html = file_get_contents('http://stackoverflow.com/');
$dom = new DOMDocument();
libxml_use_internal_errors(true);

$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

// find the element whose href value you want by XPath
$nodes = $xpath->query('//*[@class="-logo js-gps-track"]');

foreach($nodes as $href) {
// print out the href value
echo $href->getAttribute( 'href' );
}

Obviously you'd just need to amend the URL and the XPath for your specific use case.

How to get content of div ( from external url) without loading the whole website

you could do it using php aswell

<?php 
$dom = new DOMDocument;
libxml_use_internal_errors(true); //hides errors from invalid tags
$dom->loadHTMLFile('http://www.w3schools.com/php/default.asp');
$DOMxpath = new DOMXPath($dom);
$DivContent = $DOMxpath->query("//a[@class='menu_default']");
var_dump($DivContent->item(0)->textContent);

How to Extract a specific div content from a external website?

It will throw an error

No 'Access-Control-Allow-Origin' header is present on the requested resource

Reason is you can not access external domain url through XmlHttpRequest.

Alternative is create server side intermediater that pull data from weather.com and serve to your page using load or ajax method.



Related Topics



Leave a reply



Submit