Is it possible to use DOM on an external webpage? - php

I like to use the list on this page: list of teams of soccerclub
But I always get this error
Fatal error: Uncaught Error: Call to a member function find() on null in /home/u562375926/domains/lucswebsite.tech/public_html/resp/scrapvv.php:152 Stack trace: #0 {main} thrown in /home/u562375926/domains/lucswebsite.tech/public_html/resp/scrapvv.php on line 152
using this code:
<?php
include('simple_html_dom.php');
$file = 'https://www.voetbalvlaanderen.be/club/1771/ploegen';
$html = new simple_html_dom();
$html->load_file($file);
$club = $html->find('div',8)->find(h2);
echo $club. '<br>';
?>
I have used the same page in ParseHub and I get all a hrefs and the text of the corresponding spans.
Is it possible that DOM is not working on that page?

Related

PHP simple HTML DOM parser errors

I've started writing a scraper for one site that will also have a crawler, since I need to go through some links, but I'm getting this error :
PHP Fatal error: Uncaught Error: Call to a member function find() on
null in D:\Projekti\hemrank\simple_html_dom.php:1129 Stack trace:
0 D:\Projekti\hemrank\scrapeit.php(37): simple_html_dom->find('ul')
1 D:\Projekti\hemrank\scrapeit.php(19): ScrapeIt->getAllAddresses()
2 D:\Projekti\hemrank\scrapeit.php(55): ScrapeIt->run()
3 {main} thrown in D:\Projekti\hemrank\simple_html_dom.php on line 1129
When I var_dump the $html variable I get the full html with all the tags, etc, that's why it's strange to me that it says "Call to a member function find() on null", when there's actually value in the $html. Here's the part of the code that's not working :
$html = new simple_html_dom();
$html->load_file($baseurl);
if(empty($html)){echo "HTTP Response not received!<br/>\n";exit;}
$links = array();
foreach ($html->find('ul') as $ul) {
if(!empty($ul) && (count($ul)>0))
foreach ($ul->find('li') as $li) {
if(!empty($li) && (count($li)>0))
foreach ($li->find('a') as $a) {
$links[] = $a->href;
}
else
die("NOT AVAILABLE");
}
}
return $links;
}
Is this a common problem with PHP simple HTML DOM parser, is there a solution or should I switch to some other kind of scraping?
I just searched for the lib you are using, this is line 1129:
return $this->root->find($selector, $idx, $lowercase);
So your error message is telling you that $this->root inside the class is null, therefore no find() method exists!
I'm no expert on the lib, as I use the awesome DOMDocument for parsing HTML, but hopefully this should help you understand what has happened.
Also, $html will never be empty in that code of yours, you already populated it when you instantiated it!
I suggest the following change:
$html->load_file($baseurl); to $html = file_get_html($baseurl);
On my VPS server it works with $html->load_file($baseurl); but on my dedicated local server it only works with $html = file_get_html($baseurl);
This solved my problem:
- Call to a member function find() on null
- simple_html_dom.php on line 1129

Uncaught Error: Call to undefined method DB::execute()

I am using indieteq-php-my-sql-pdo-database-class I found on GitHub
I have created a little page to try and display some database information as seen below, however I receive the error below my test page script below;
My Test Page
<?php
require("Db.class.php");
$db = new Db();
$db->query("SELECT * FROM faction_territories");
if ($db->execute()) {
while ($row = $db->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>
<td>'.$row['territory_id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['density'].'</td>
<td>'.$row['sector'].'</td>
<td>'.$row['size'].'</td>
<td>'.$row['respect'].'</td>
<td>'.$row['faction_name'].'</td>
</tr>';
}
echo '</table>';
}
?>
Error
Fatal error: Uncaught Error: Call to undefined method DB::execute() in
/var/www/.../index.php:7 Stack trace: #0 {main} thrown in
/var/www/.../index.php on line 7
Questions
As you'll be able to tell, I'm somewhat new to PHP & PDO. Some nice pointers to where I am going wrong would be nice with any example code please.
The $db->query already execute the query. You just need to iterate over the result.
See here: https://github.com/wickyaswal/indieteq-php-my-sql-pdo-database-class/blob/master/Db.class.php#L189

file_get_html error, not working

I'm using Simple HTML Dom to try scrape a HTML table.
I follow their instructions and have looked at many other code examples, but the file_get_html just doesn't seem to work.
Here is my code:
<?php
// Simple HTML Dom Parser
include('simple_html_dom.php');
//$worlds = ["Amera", "Antica", "Astera", "Aurera", "Aurora", "Bellona", "Belobra", "Beneva", "Calmera", "Calva", "Calvera", "Candia", "Celesta", "Chrona", "Danera", "Dolera", "Efidia", "Eldera", "Ferobra", "Fidera", "Fortera", "Garnera", "Guardia", "Harmonia", "Honera", "Hydera", "Inferna", "Iona", "Irmada", "Julera", "Justera", "Kenora", "Kronera", "Laudera", "Luminera", "Magera", "Menera", "Morta", "Mortera", "Neptera", "Nerana", "Nika", "Olympa", "Osera", "Pacera", "Premia", "Pythera", "Quilia", "Refugia", "Rowana", "Secura", "Serdebra", "Shivera", "Silvera", "Solera", "Tavara", "Thera", "Umera", "Unitera", "Veludera", "Verlana", "Xantera", "Xylana", "Yanara", "Zanera", "Zeluna"];
//foreach ($worlds as $world) {
// All HTML from the online list
$html = file_get_html('https://secure.tibia.com/community/?subtopic=worlds&world=Antica');
// Search for the online list table content
foreach ($html->find('tr[class=Table2]') as $row) {
$name = $row->find('td', 0)->plaintext;
$level = $row->find('td', 1)->plaintext;
$vocation = $row->find('td', 2)->plaintext;
echo $name . ' | ' . $level . ' | ' . $vocation . '<br>';
}
//}
?>
And I get these errors:
Warning: file_get_contents(): stream does not support seeking in D:\xampp\htdocs\simple_html_dom.php on line 76
Warning: file_get_contents(): Failed to seek to position -1 in the stream in D:\xampp\htdocs\simple_html_dom.php on line 76
Fatal error: Uncaught Error: Call to a member function find() on boolean in D:\xampp\htdocs\index.php:13 Stack trace: #0 {main} thrown in D:\xampp\htdocs\index.php on line 13
What am I doing wrong?
The table I am trying to scrape is the "Players Online" table on:
https://secure.tibia.com/community/?subtopic=worlds&world=Antica
Try this:
$html = str_get_html(file_get_contents($url));
This is a simple_html_dom library problem with the latest versions of PHP.
To correct it, simply change "$offset = -1," to "$offset = 0," in the parameters of the "file_get_html" function in the "simple_html_dom.php" file.
I don't know much about simpledom but i think you might need to use a more robust library like https://github.com/FriendsOfPHP/Goutte

When I am going List task from task queue(pull), I get an error

When I am going List task from task queue(pull) at GAE, I get an error. Please give me solution:
I am using AEQ library: https://github.com/tomwalder/php-appengine-pull-queue
My code is
include_once 'AEQ/Task.php';
include_once 'AEQ/Queue.php';
//$obj_task = new AEQ\Task();
$obj_queue = new AEQ\Queue('ondemand-image-task-queues');
// List Tasks
foreach($obj_queue->listTasks() as $obj_task) {
echo $obj_task->getName();
}
I got an error
Fatal error: Uncaught exception 'RuntimeException' with message
'Failed to execute call [QueryTasks] with: 9 (PERMISSION_DENIED)' in
/base/data/home/apps/s~apigraymatics/1.390942967888630935/public/pull/AEQ/Queue.php:223
Stack trace: #0
/base/data/home/apps/s~project/1.390942967888630935/public/pull/AEQ/Queue.php(197):
AEQ\Queue->makeCall('QueryTasks',
Object(google\appengine\TaskQueueQueryTasksRequest),
Object(google\appengine\TaskQueueQueryTasksResponse)) #1
/base/data/home/apps/s~project/1.390942967888630935/public/pull/listtask.php(13):
AEQ\Queue->listTasks() #2 {main} thrown in
/base/data/home/apps/s~project/1.390942967888630935/public/pull/AEQ/Queue.php
on line 223
Please help me....

PHP: DOMNode::appendChild to an array of Elements

I am using DOMDocument to parse an XML file. I loop through the different Elements and see if any of them is missing and I fill an array with a createElement, with the error message. At the end I'm trying to appendChild that array but I always get the same error message:
Uncaught exception 'DOMException' with message 'Wrong Document Error'
DOMNode->appendChild(Object(DOMElement))
1 {main}
thrown in /xxx/xxx.php on line 235
PHP Fatal error: Call to undefined method DOMElement::item() in /xxx/xxx.php on line 235.
the code is as follow:
$SMQuery = new DOMDocument();
$SMQuery->loadXML($params);
$response = $SMQuery->createElement('SMreply');
$errors = array();
if (!$reqtyp = $SMQuery->getElementsByTagName("tag1"))
{$errors[] = $SMQuery->createElement('error', 'tag1 Element is missing');}
if (!$reqtyp = $SMQuery->getElementsByTagName("tag2"))
{$errors[] = $SMQuery->createElement('error', 'tag2 Element is missing');}
......
if(!empty($errors))
{
foreach($errors as $error) {
$response->appendChild($error); <==== this line is causing the error !!!
}
}
Any help is much appreciated.
Cheers,
Riki.
You don't show where $response is being defined, but if it's the result of another new DOMDocument(), then that explains you error - you can't add nodes from one DOM object to another directly. It has to be imported first via ->importNode(). Only after that can you actually append it.

Categories