There are many posts on this topic but I did not get the required answer. Hence, I am here.
I have been getting Notice: Trying to get property of non-object in /opt/lampp/htdocs/amit/crawlnepalstock.php on line 49 error in my php page.
Here is my code
<?php
include_once('simple_html_dom.php');
error_reporting(E_ALL);
$html = file_get_html('http://nepalstock.com/datanepse/index.php');
$indexarray = array('s_no','stocksymbol', 'LTP', 'LTV', 'point_change', 'per_change', 'open','high', 'low', 'volume','prev_close');
$stocks = array();
$maincount = 0;
$tables = $html->find('table[class=dataTable]');
$str = $html->plaintext;
$matches = array();
foreach ($tables[0]->find('tr') as $elementtr) {
$count = 0;
$temp = array();
$anchor = $elementtr->children(1)->find('a',0);
$splits = preg_split('/=/', $anchor->href); **//line 49**
$temp['stocksymbol'] = isset($splits[1]) ? $splits[1] : null;
$temp['fullname'] = $elementtr->children(1)->plaintext;
$temp['no_of_trans'] = $elementtr->children(2)->plaintext;
$temp['max_price'] = $elementtr->children(3)->plaintext;
$temp['min_price'] = $elementtr->children(4)->plaintext;
$temp['closing_price'] = $elementtr->children(5)->plaintext;
$temp['total_share'] = $elementtr->children(6)->plaintext;
$temp['amount'] = $elementtr->children(7)->plaintext;
$temp['previous_close'] = $elementtr->children(8)->plaintext;
$temp['difference'] = $elementtr->children(9)->plaintext;
$stocks[] = $temp;
}
$html->clear();
unset($html);
echo '<pre>';
print_r($stocks);
echo '</pre>';
?>
I have not included simple_html_dom.php class as it is quite long. Your opinions are very much appreciated. You can find simple_html_dom.php file online in case http://sourceforge.net/projects/simplehtmldom/files/
You are trying to access property of non-object or from null object. e.g.
$obj = null;
echo $boject->first_name // this will produce same error as you are getting.
// another example may be
$obj = array();
echo $obj->first_name; // this will also produce same error.
In code sample Line 49 is not clear so you should check yourself this type of error on line 49.
This is happening because there is no longer a td[align="center"] tag found in the google.com document. Perhaps it was there when the code was first written.
So, what the others are saying about a non-object is true, but because the HTML was not found, there is not an object to use the ->plaintext method on.
As of 12/11/2020, if you change the URL found in line 6 of example_basic_selector.php to this:
$html = file_get_html('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_td_align_css');
And change this line
echo $html->find('td[align="center"]', 1)->plaintext . '';
to:
echo $html->find('td style="text-align:right"', 1)->plaintext. '';
the error will go away because the text it searches for is found, and thus the method works as intended.
Related
I've been trying to fix this error for the longest time now, and I just can't seem to fix it.
I'm trying to get an article image, url, and url title. For some reason I keep getting the above error for this code:
<?php
$html = file_get_html("http://articlesite.com/");
if($html){
foreach ($html->find('.index_item a img') as $div) {
$articlePoster = $div->src;
$grabURL = $html->find('.index_item a');
/*Error Here -->*/$articleURL = $grabURL->href;
/*And Here -->*/$rawTitle = $grabURL->title;
echo '<div class="articleFrame"><img src="'.$articlePoster.'" width="125" height="186"/><br><p class="title">'.$rawTitle.'</p></div>';
}
}else{
echo '<h1>'."Sorry.".'</h1>';
}
?>
Any ideas? Thanks.
$html->find('xxxxx') returns an array, so you need to iterate through it -- i.e.
foreach ($html->find('.index_item a img') as $div) {
$articlePoster = $div->src;
foreach ($html->find('.index_item a') as $grabURL) {
$articleURL = $grabURL->href;
$rawTitle = $grabURL->title;
(etc.)
I am trying to scrape this product table,
http://www.dropforyou.com/search2.php?mode=search&posted_data%5Bcategoryid%5D=2&posted_data%5Bsearch_in_subcategories%5D=on
I need the product id, the quantity and the price.
Since the site uses cookies and a post form I am grabbing the site with CURL. Which works fine. I am then loading that into simple html dom with $html = str_get_html($content);
I have been able to load all the table values into an array, however I can't label them. They just come in as 0,1,2 and I can't tell what's what.
I tried using a different method posted here on stackoverflow, however it gives me Fatal error: Call to a member function find() on a non-object in
My working code that isn't labeled
$content = curlscraper($urltoscrape);
$html = str_get_html($content);
$tds = $html->find('table',2)->find('td');
$num = NULL;
foreach($tds as $td)
{
$num[] = $td->plaintext;
}
echo '<pre>';
var_dump ($num);
echo '</pre>';
The code I found on Stackoverflow that just gives me Fatal error: Call to a member function find() on a non-object in
$content = curlscraper($urltoscrape);
$html = str_get_html($content);
foreach($html->find('tr',2) as $page)
{
$item['sku'] = $page->find('td',0)->plaintext;
$item['product'] = $page->find('td',1)->plaintext;
$item['Qty'] = $page->find('td',2)->plaintext;
$item['description'] = $page->find('td',3)->plaintext;
$item['price'] = $page->find('td',4)->plaintext;
$table[] = $item;
}
print_r($table);
Try initialize variable for your foreach function and then use your code. But you don't say which line created that error?
$line = $html->find('tr',2);
foreach($line as $page)
{
//var_dump($page) //You can check array
$item['sku'] = $page->find('td',0)->plaintext;
$item['product'] = $page->find('td',1)->plaintext;
$item['Qty'] = $page->find('td',2)->plaintext;
$item['description'] = $page->find('td',3)->plaintext;
$item['price'] = $page->find('td',4)->plaintext;
$table[] = $item;
}
Trying to Parse the price from a site. I can already retrieve the title from the source but i get a Notice when i attempt to scrape the price.
Notice: Undefined offset: 1
Here's the code:
<?php
$file_string = file_get_contents('http://finance.google.com');
preg_match('/<title>(.*)<\/title>/i', $file_string, $title);
$title_out = $title[1];
preg_match('~<span id="ref_658274_l">(.*)</span>~', $file_string, $price);
//error on the line below
$price_out = $price[1];
?>
<?php echo "$title_out"; ?>
<?php echo "$price_out"; ?>
Parsing HTML may be more successful with DOMDocument
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents('http://finance.google.com'));
$titleElems = $doc->getElementsByTagName('title');
if ($titleElems->length) {
$title = $titleElems->item(0)->nodeValue;
}
$priceElem = $doc->getElementById('ref_658274_l');
if ($priceElem != null) {
$price = $priceElem->nodeValue;
}
Your regular expression doesn't match. When using the result, you should always validate that the index you are using, in this case 1, is within the bounds of your array.
how to write this correctly?
for($i=2;$i<5;$i++)
{
$items{$i} = $doc{$i}->getElementsByTagName('url');
}
got an error:
Fatal error: Call to a member function getElementsByTagName() on a non-object
Thanks,
bye
try :
$ndoc = 'doc'. $i;
$items[$i] = $$ndoc->getElementsByTagName('url');
or:
$ndoc = 'doc'. $i;
$items[$i] = ${$ndoc}->getElementsByTagName('url');
This test worked for me:-
$html1 = "<html><head><title>Test</title></head><body><p>Doc1 P1</p><p>Doc1 P2</p></body></html>";
$html2 = "<html><head><title>Test</title></head><body><p>Doc2 P1</p><p>Doc2 P2</p></body></html>";
$doc1 = new DOMDocument();
$doc1->loadHTML($html1);
$doc2 = new DOMDocument();
$doc2->loadHTML($html2);
for($i=1;$i<3;$i++){
var_dump(${'doc'.$i}->getElementsByTagName('p'));
}
So, your code should look like this:-
for($i=2;$i<5;$i++)
{
${'items'.$i} = ${'doc'.$i}->getElementsByTagName('url');
}
Unless you mean for $items to be an array, in which case it should look like this:-
for($i=2;$i<5;$i++)
{
$items[] = ${'doc'.$i}->getElementsByTagName('url');
}
I am having problem with the yahoo search API, sometimes it works and sometimes don't why I am getting problem with that
I am using this URL
http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start
The code is given below:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<? $search = $_GET["search"];
$replace = " "; $with = "+";
$search = str_replace($replace, $with, $search);
if ($rs =
$rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start")
)
{ }
// Go through the list powered by the search engine listed and get
// the data from each <item>
$colorCount="0";
foreach($rs['items'] as $item) { // Get the title of result
$title = $item['title']; // Get the description of the result
$description = $item['description']; // Get the link eg amazon.com
$urllink = $item['guid'];
if($colorCount%2==0) {
$color = ROW1_COLOR;
} else {
$color = ROW2_COLOR;
}
include "resulttemplate.php"; $colorCount++;
echo "\n";
}
?>
Sometimes it gives results and sometimes don't. I get this error usually
Warning: Invalid argument supplied for foreach() in /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php on line 14
Can anyone help..
The error Warning: Invalid argument supplied for foreach() in /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php on line 14 means the foreach construct did not receive an iterable (usually an array). Which in your case would mean the $rs['items'] is empty... maybe the search returned no results?
I would recommended adding some checks to the results of $rss->get("...") first, and also having an action for when the request fails or returns no results:
<?php
$search = isset($_GET["search"]) ? $_GET["search"] : "default search term";
$start = "something here"; // This was left out of your original code
$colorCount = "0";
$replace = " ";
$with = "+";
$search = str_replace($replace, $with, $search);
$rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start");
if (isset($rs) && isset($rs['items'])) {
foreach ($rs['items'] as $item) {
$title = $item['title']; // Get the title of the result
$description = $item['description']; // Get the description of the result
$urllink = $item['guid']; // Get the link eg amazon.com
$color = ($colorCount % 2) ? ROW2_COLOR : ROW1_COLOR;
include "resulttemplate.php";
echo "\n";
$colorCount++;
}
}
else {
echo "Could not find any results for your search '$search'";
}
Other changes:
$start was not declared before your $rss->get("...") call
compounded the $color if/else clause into a ternary operation with fewer comparisons
I wasn't sure what the purpose of the if ($rs = $rss->get("...")) { } was, so I removed it.
I would also recommend using require instead of include as it will cause a fatal error if resulttemplate.php doesn't exist, which in my opinion is a better way to detect bugs than PHP Warnings which will continue execution. However I don't know you whole situation so it might not be of great use.
Hope that helps!
Cheers