I have a url:
http://www.indeed.com/viewjob?jk=daddefef363643d7&qd=E8dXiB4h7yBMgEwoEDfyDF2ACaqK5NNcKe-lg0a0QeWlgGT7hwsgagao8YFkybxtaLZJqFprtIWhTxIjvWFBLUePVQb0Chqftd-uc7_Pfa4LB2pHYt-YP2NYagtBg9Lp&atk=1a4sk4spi1c0o5la&utm_source=publisher&utm_medium=organic_listings&utm_campaign=affiliate
I want to extract href value of anchor for view and apply
my code is:-
$dom = new DOMDocument();
#$dom->loadHtml($html);
$xpath = new DOMXpath($dom);
$applylink = $xpath->query("//*[#class='job-footer-button-row']/a");
if(!is_null($applylink)){
$this->view->applylink = $applylink->item(0)->getAttribute('href');
}
But it always shows below error:
Fatal error: Call to a member function getAttribute() on a non-object
This happens because DOMXPath::query does not return null when it finds no matches. Please read the documentation to see what it returns, and that should allow you to correct your code.
Related
as the title says I'm trying to get code from with id counterResult it's an external website this is my code so far:
$doc = new DOMDocument();
$doc->loadHTML($url);
$doc->preserveWhiteSpace = false;
$resultCounter = $doc->getElementById('resultCounter');
echo $resultCounter->nodeValue;
The error that I get on the last line (with echo) is ) Notice: Trying to get property of non-object in C:\wamp\www\wheelofporn\test.php on line 10
I'm parsing a exterior html (http://www.amazon.com/Toshiba-Satellite-C55-A5245-15-6-Inch-Horizon/dp/B00D78PZE8/ref=lp_9277875011_1_1?s=pc&ie=UTF8&qid=1400886357&sr=1-1) where I have a element like this:
<span id="priceblock_ourprice" class="a-size-medium a-color-price">$429.99</span>
and a php with the following code:
$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTMLFile($url);
libxml_clear_errors();
$links = $dom->getElementsById('priceblock_ourprice');
foreach ($links as $link ) {
echo "- ".$link->nodeValue."<br>";
}
But I'm getting the following error:
Fatal error: Call to undefined method DOMDocument::getElementsById()
Anyone could tell me what I'm doing wrong?
Thanks!
getElementsById() is not a method of DOMDocument, you should try getElementById() instead. I don't even think two elements can have the same id, so you won't be able to get a collection (array) based on id.
Ok, so I don't quite understand this, seems that Firebug in Firefox was showing me the wrong ID, I used the following code to get the Id of the different spans and the right one was:
$dom = new DOMDocument();
libxml_use_internal_errors(TRUE);
$dom->loadHTMLFile($url);
libxml_clear_errors();
$nodes = $dom->getElementsByTagName('span');
foreach($nodes as $node) {
echo $node->getAttribute('id'). '->'.$node->textContent.'<br>';
}
and it returned a different id for the field that I was looking for, I guess I had some error at some point, really sorry for waisting your time.
I'm trying to parse some html that is inside an external .dat file.
I would normaly use the follwing code:
$html = new DOMDocument();
$html->loadHTMLFile('http://www.bvl.com.pe/includes/cotizaciones_todas.dat');
$xpath = new DOMXPath($html);
$path = '/somepath';
$nodelist = $xpath->query($path);
echo $nodelist->item(0)->nodeValue;
But I'm getting this error:
DOMDocument::loadHTMLFile(): htmlParseEntityRef: expecting ';' in http://www.bvl.com.pe/includes/cotizaciones_todas.dat, line: 15
I know that the problem is the loadHTMLFile, I tried using load or loadXML but it's not working neither.
Any help would be appriciated.
UPDATE
To solve the problem I had to handle the errors using libxml_use_internal_errors(TRUE).
Now I've a new problem, I want to count how many <tr> tags are inside the table. I'm using the following code:
$html = new DOMDocument();
libxml_use_internal_errors(TRUE);
$html->loadHTMLFile('http://www.bvl.com.pe/includes/cotizaciones_todas.dat');
libxml_clear_errors();
$xpath = new DOMXPath($html);
$tbody = $html->getElementsByTagName('tbody')->item(0);
$path = 'count(tr)';
$trCount = $xpath->evaluate($path,$tbody);
But I'm getting this error msg: PHP Catchable fatal error: Argument 2 passed to DOMXPath::evaluate() must be an instance of DOMNode, null given I already used the same code with other files and everything worked fine, but in this case it's not working, maybe because the html is broken?
I am using the following code to get the first of a page. However I can not get it. What am I missing here ?
$doc = new DOMDocument();
$doc->loadhtmlfile("");
$xpath = new DOMXpath($doc);
$descr = $xpath->query('//div[#class="description"]');
print_r($descr);
query() returns a DOMNodeList, to get the <div> DOMNode, you need to get it from the list:
$descr = $xpath->query('//div[#class="description"]')->item(0);
Now, $descr contains a DOMNode of the first <div> with class description.
Here i parse some data from a webpage.
I want to write it to an file. It all works ok when i use some test strings in
$xml->addChild('alink', 'test');
But when i try and write in the data i actually need to use
$xml->addChild('alink', $value);
It doesnt work.
Message is :
Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: unterminated entity reference .wvx= in C:\Documents and Settings\Owner\My Documents\Downloads\XAMPP_1.7.1\xampp\htdocs\PhpTest2\index.php on line 96
Complete code. Why does addChild not let me use a var there as agrument 2 in that method? And what is the word around to getting that working. Can find no explanation on php.net
$dom = new DOMDocument();
#$dom->loadHtml($html);
$xpath = new DOMXPath($dom);
$articleList = $xpath->query("//body/div/div/div/table/tbody/tr/td/a");
$xml = new SimpleXmlElement('<links></links>');
$xml->addChild('dvd');
foreach ($articleList as $art)
{
$value = $art->getAttribute('href');
$xml->addChild('alink', $value);
}
$xml->asXML('/simplexml_create.xml');
Many Thanks,
-Code