Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse a website with PHP?

Tags:

dom

php

parsing

I want to make a website with news just like my other site appears on the Internet.

I haven't got enough content. So I want to get content from another news website (Of course, I got permission to copy their content from their site).

For this I used Simple HTML DOM, and it's progressing very well on my other site, but as I apply my script on the release site, it doesn't work (outputs nothing).

Here is the link I want to get the data from: hesspress.com.

Code:

require_once 'simple_html_dom.php';
$lien='http://www.hesspress.com';
$html=new simple_html_dom();
$html->load_file($lien);
$k=$html->find('a',0)->href;
print_r($k);
like image 619
Ti Amo Laky Avatar asked May 26 '26 05:05

Ti Amo Laky


1 Answers

This is what I would use, you can modify it for your own purposes.

It uses the built-in PHP DOM.

Code:

<?php
$url = 'http://acid3.acidtests.org';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($handle);
libxml_use_internal_errors(true); // Prevent HTML errors from displaying
$doc = new DOMDocument();
$doc->loadHTML($html);
$link = $doc->getElementsByTagName('a')->item(0);
echo 'Link text: ' . $link->nodeValue;
echo '<br />';
echo 'Link URL: ' . $link->getAttribute('href');
?>
like image 84
uınbɐɥs Avatar answered May 27 '26 19:05

uınbɐɥs