Hello everyone i am fetching data by using simple html dom
This is my code of php which is fetching data from site
include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file($this->main_url.$lin->link);
if($html){
//check if language heading h2 exist then process forward
if($html->find('h2.channel-title',0)){
fetch data from tables
}
}
This line if($html->find('h2.channel-title',0)) finding h2.channel-title in find function of simple html dom give me a fatal error when h2.channer-title is not exist
In many pages <h2 class="channel-title"> English Links</h2> exists so i have code according to them and process further in my foreach loop it's working fine and fetched all data.
But
when <h2 class="channel-title">English Links</h2> tag is not exist it give me an error
Fatal error: Call to a member function find() on a non-object in C:\xampp\apps\wordpress\htdocs\wp-content\plugins\autobot\engine\simple_html_dom.php on line 1113
Please help me i am stuck in it need help thank you. i want if h2.channel-title exist run my foreach code else run another but don't give an error its stop my whole script. :(
this might help.
$html = new simple_html_dom();
$html->load_file($this->main_url.$lin->link);
if($html) {
$var = $html->find('h2.channel-title',0);
if(isset($var)) {
fetch data from tables
} else{
//do something
}
}
var_dump($html);
Which library you are using?
Related
I would like to scrape the table t-02 from https://wyniki.tge.pl/wyniki/rdn/ with HTML DOM Parser.
I created simple code but I was getting errors:
Fatal error: Call to a member function find() on null in /Users/piotrek/Sites/foo/index.html on line 3
My first code:
<?php
include ("simple_html_dom.php");
$html=file_get_html("https://wyniki.tge.pl/pl/wyniki/rdn/");
$tables=$html->find("table[#class=t-02]");
foreach($tables->find("tr") as $a) {
foreach($a->find("td") as $element) {
echo $element;
}
}
?>
I changed the code to print innertext and it worked:
<?php
include_once ("simple_html_dom.php");
$html=file_get_html("https://wyniki.tge.pl/pl/wyniki/rdn/");
$title=$html->find("table[#class=t-02]",0)->innertext;
echo $title
?>
I changed only code so what was wrong with first approach? What was the reason of fatal error?
The error is saying that the $html object is null, so there is nothing to perform the find method against. Do you have the simple_html_dom.php file in the same directory?
Can anyone please help me in finding out what have I done wrong in the code given below.
I have a PHP variable named news_content whose value is the following html...
<p><img src="./images/image1.jpeg" alt=""></p>
This value for variable news_content is obtained from a database query.
The function below creates a DOMDocument object using the variable news_content :
public function convert_to_tinymce_data($news_content)
{
$dom=new DOMDocument();
$dom->loadHTML($news_content);
$img_nodes=$dom->getElementsByTagName('img');
foreach($img_nodes as $link)
{
$img_link=$link->getAttribute('src');
echo $link->getAttribute('src');
}
}
But nothing is being echoed (receiving a blank page).
I've tryed this code and it works:
$n = '<p><img src="./images/image1.jpeg" alt=""></p>';
function convert_to_tinymce_data($news_content){
$dom=new DOMDocument();
$dom->loadHTML($news_content);
$img_nodes=$dom->getElementsByTagName('img');
foreach($img_nodes as $link) {
$img_link=$link->getAttribute('src');
echo $img_link;
}
}
convert_to_tinymce_data($n);
I see you have public function, but I don't see class. If it's global function, not a method of a class, that's the reason why it doesn't work. Or you don't call function properly.
(What you have there is function definition... it executes after you call it... Having same variable names in your function's definition and outside is bad practice and can be easily turn into problem)
Just to add: Check you're error_log file and let us know is there something interesting in it...
I am using this library (PHP Simple HTML DOM parser) to parse a link, here's the code:
function getSemanticRelevantKeywords($keyword){
$results = array();
$html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=");
foreach($html->find('span') as $e){
$results[] = $e->plaintext;
}
return $results;
}
but I am getting this error when I output the results:
Fatal error: Call to a member function find() on a non-object in
/var/www/vhosts/efamous.de/subdomains/sandbox/httpdocs/getNewTrusts.php
on line 25
(line 25 is the foreach loop), the odd thing is that it outputs everything (at least seemingly) correctly but I still get that error and can't figure out why.
The reason for this error is: the simple HTML DOM does not return the object if the size of the response from url is greater than 600000.
You can void it by changing the simple_html_dom.php file. Remove strlen($contents) > MAX_FILE_SIZE from the if condition of the file_get_html function.
This will solve your issue.
You just need to increase CONSTANT MAX_FILE_SIZE in file simple_html_dom.php.
For example:
define('MAX_FILE_SIZE', 999999999999999);
This error usually means that $html isn't an object.
It's odd that you say this seems to work. What happens if you output $html?
I'd imagine that the url isn't available and that $html is null.
Edit:
Looks like this may be an error in the parser. Someone has submitted a bug and added a check in his code as a workaround.
Before file_get_html/load_file method, you should first check if URL exists or not.
If the URL exists, you pass one step.
(Some servers, service a 404 page a valid HTML page. which has propriate HTML page structure like body, head, etc. But it has only text "This page couldn'!t find. 404 error bla bla..)
If URL is 200-OK, then you should check whether fetched thing is object and whether nodes are set.
That's the code i used in my pages.
function url_exists($url){
if ((strpos($url, "http")) === false) $url = "http://" . $url;
$headers = #get_headers($url);
// print_r($headers);
if (is_array($headers)){
if(strpos($headers[0], '404 Not Found'))
return false;
else
return true;
}
else
return false;
}
$pageAddress='http://www.google.com';
if ( url_exists($pageAddress) ) {
$htmlPage->load_file( $pageAddress );
} else {
echo 'url doesn t exist, i stop';
return;
}
if( $htmlPage && is_object($htmlPage) && isset($htmlPage->nodes) )
{
// do your work here...
} else {
echo 'fetched page is not ok, i stop';
return;
}
For those arriving here via a search engine (as I did), after reading the info (and linked bug-report) above, I started some code-prodding and ended up fixing my problems with 2 extra checks after loading the dom;
$html = file_get_html('<your url here>');
// first check if $html->find exists
if (method_exists($html,"find")) {
// then check if the html element exists to avoid trying to parse non-html
if ($html->find('html')) {
// and only then start searching (and manipulating) the dom
}
}
I'm having the same error come up in my logs and apart from the solutions mentioned above, it could also be that there is no 'span' in the document. I get the same error when searching for divs with a particular class that doesn't exist on the page, but when searching for something that I know exists on the page, the error doesn't pop up.
your script is OK.
I receive this error when it doase not find the element that i'm looking for on that page.
In your case, please check if the page that you are accessing it has 'SPAN' element
Simplest solution to this problem
if ($html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=") {
} else {
// do something else because couldn't find html
}
Error means, the find() function is either not defined yet or not available. Make sure you have loaded or include related function.
I am using this library (PHP Simple HTML DOM parser) to parse a link, here's the code:
function getSemanticRelevantKeywords($keyword){
$results = array();
$html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=");
foreach($html->find('span') as $e){
$results[] = $e->plaintext;
}
return $results;
}
but I am getting this error when I output the results:
Fatal error: Call to a member function find() on a non-object in
/var/www/vhosts/efamous.de/subdomains/sandbox/httpdocs/getNewTrusts.php
on line 25
(line 25 is the foreach loop), the odd thing is that it outputs everything (at least seemingly) correctly but I still get that error and can't figure out why.
The reason for this error is: the simple HTML DOM does not return the object if the size of the response from url is greater than 600000.
You can void it by changing the simple_html_dom.php file. Remove strlen($contents) > MAX_FILE_SIZE from the if condition of the file_get_html function.
This will solve your issue.
You just need to increase CONSTANT MAX_FILE_SIZE in file simple_html_dom.php.
For example:
define('MAX_FILE_SIZE', 999999999999999);
This error usually means that $html isn't an object.
It's odd that you say this seems to work. What happens if you output $html?
I'd imagine that the url isn't available and that $html is null.
Edit:
Looks like this may be an error in the parser. Someone has submitted a bug and added a check in his code as a workaround.
Before file_get_html/load_file method, you should first check if URL exists or not.
If the URL exists, you pass one step.
(Some servers, service a 404 page a valid HTML page. which has propriate HTML page structure like body, head, etc. But it has only text "This page couldn'!t find. 404 error bla bla..)
If URL is 200-OK, then you should check whether fetched thing is object and whether nodes are set.
That's the code i used in my pages.
function url_exists($url){
if ((strpos($url, "http")) === false) $url = "http://" . $url;
$headers = #get_headers($url);
// print_r($headers);
if (is_array($headers)){
if(strpos($headers[0], '404 Not Found'))
return false;
else
return true;
}
else
return false;
}
$pageAddress='http://www.google.com';
if ( url_exists($pageAddress) ) {
$htmlPage->load_file( $pageAddress );
} else {
echo 'url doesn t exist, i stop';
return;
}
if( $htmlPage && is_object($htmlPage) && isset($htmlPage->nodes) )
{
// do your work here...
} else {
echo 'fetched page is not ok, i stop';
return;
}
For those arriving here via a search engine (as I did), after reading the info (and linked bug-report) above, I started some code-prodding and ended up fixing my problems with 2 extra checks after loading the dom;
$html = file_get_html('<your url here>');
// first check if $html->find exists
if (method_exists($html,"find")) {
// then check if the html element exists to avoid trying to parse non-html
if ($html->find('html')) {
// and only then start searching (and manipulating) the dom
}
}
I'm having the same error come up in my logs and apart from the solutions mentioned above, it could also be that there is no 'span' in the document. I get the same error when searching for divs with a particular class that doesn't exist on the page, but when searching for something that I know exists on the page, the error doesn't pop up.
your script is OK.
I receive this error when it doase not find the element that i'm looking for on that page.
In your case, please check if the page that you are accessing it has 'SPAN' element
Simplest solution to this problem
if ($html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=") {
} else {
// do something else because couldn't find html
}
Error means, the find() function is either not defined yet or not available. Make sure you have loaded or include related function.
Some basic background ...
I have a form that enters data to an xml file and another page that displays the data from teh xml depending that it meets the requirements . All of this I have managed to get done and thanks to a member on here I got it to show only the data as long as it has todays date and status is out . But I am left with the problem of trying to sort an if statement which needs to show data if it has it or show another div if not .
My Code ...
$lib = simplexml_load_file("sample.xml");
$today = date("m/d/y");
$query = $lib->xpath("//entry[.//date[contains(., '$today')]] | //entry[.//status[contains(., 'out')]]");
foreach($query as $node){
echo "<div id='one'>$node->name</div>
<div id='two'>$node->notes</div>
<div id='three'><div class='front'>$node->comments</div></div>";
}
So to reiterate if query returns matched data do the foreach else show another div
I only wish to know the right code for the if else statement if soneone could help with this I would be very grateful and will up vote any answer as soon as I have the reputation in place . I also apologise in advance if the question has been asked before or if it is too vague thanks again .
If xpath fails to resolve the path, it will return false (see here). Wrap the foreach loop in a simple check:
if( $query ) {
foreach($query as $node){
...
}
}
else {
// Echo the special div.
}
Since PHP is loose typed, if xpath happens to return an empty array, this check will also handle that case. Be aware that if the xpath call does return false, there may be a separate error at play that may require additional or alternative handling.