How to get content text of div by simple html dom - php - php

I get the bottom html code by simple dom html (file_get_html('http://example.com'))
<div id="ship" class="fe" data-feature-name="box" data-cel-widget="sox">
<div class="a-medium b-di">
<div id="mer-info" class="a-section a-spacing-mini">
Hello World
<span class="">
</span>
</div>
</div>
</div>
How can I get 'Hello World" content text?
I tried a lot of things for example bottom text, but that gave me 'NULL'
$html->find('div[id="mer-info"]',0);
$html->find("div#mer-info");
$html->find("div#mer-info")->plaintext;
$html->find('div[id="mer-info"]')->innertext;
and ...
But I got NULL still!

You only passed the second argument (0) to find method where you used div[id="mer-info"] as selector, which seems not to be recognized by find method. Try the following:
require 'simple_html_dom.php';
$html =<<<html
<div id="ship" class="fe" data-feature-name="box" data-cel-widget="sox">
<div class="a-medium b-di">
<div id="mer-info" class="a-section a-spacing-mini">
Hello World
<span class="">
</span>
</div>
</div>
</div>
html;
$dom = str_get_html($html);
$elem = $dom->find('#mer-info', 0);
print $elem->plaintext;
print "\n";
$elem = $dom->find('div#mer-info', 0);
print $elem->plaintext;

Related

Can anyone help me by getting The plain text from a div placed in an html file

i am a facing an error while using php simple html dom . I am working on my school project to get the plain text from a following div which is situated in an html file
<div class="product-detail__TextPrice-sc-1k47nh4-0 hECROB d-flex align-items-center justify-content-md-start">
<div class="mb-0 weight-bold price">
₹<!-- --> <!-- -->126.00
</div>
<div class="mrp ml-md-4 ml-lg-2 pd-mrp">
₹ 150.00
</div>
<div class=" w-50 margin-text ml-md-4 ml-lg-2">
<span class="main-slab main-slab-detail">
1+pc
</span>
Margin ₹ 24.00 | 16.00%
</div>
</div>
This div is present in an html file and we have got only the url to file and we have to first bring the mrp cost of item in div class mrp ml-md-4 ml-lg-2 pd-mrp and cost from class
mb-0 weight-bold price
I was using php simple html dom to do the process but i am getting a huge array and I can't figure out what to do now . Can anyone please guide me out ,. The code which i am using is below
<?php
include('simple_html_dom.php');
$cl=file_get_contents($url);
$html=new simple_html_dom();
$html->load($cl);
$ret = $html->find('div[class=mrp ml-md-4 ml-lg-2 pd-mrp]');
var_dump($ret);
?>
Thanks in advance
Try this script.
Because $html->find('div.pd-mrp') return multiple elements.
$html=new simple_html_dom();
$html->load($cl);
foreach($html->find('div.pd-mrp') as $elements) {
echo $elements->plaintext;
break;
}
Final answer :
The target value is a price on a product page. This value is not served at first call, so simple_html_dom can't access it. The workaround would be to scrape the frontend page in a browser extension for example, and to get the value via javascript. This way, you could get your value and process it or send it back to php, if you need.
First answer :
I've made a text text on my server :
<?php
include('simple_html_dom.php');
$cl = <<< TEST
<div class="product-detail__TextPrice-sc-1k47nh4-0 hECROB d-flex align-items-center justify-content-md-start">
<div class="mb-0 weight-bold price">
₹<!-- --> <!-- -->126.00
</div>
<div class="mrp ml-md-4 ml-lg-2 pd-mrp">
₹ 150.00
</div>
<div class=" w-50 margin-text ml-md-4 ml-lg-2">
<span class="main-slab main-slab-detail">
1+pc
</span>
Margin ₹ 24.00 | 16.00%
</div>
</div>
TEST;
$html=new simple_html_dom();
$html->load($cl);
$ret = $html->find("div.mrp.ml-md-4.ml-lg-2.pd-mrp",0)->plaintext;
print_r($ret);
?>
it outputs :
₹ 150.00

Getting element in PHP - PHP Simple HTML DOM Parser

Can you help me with the resolution below?
I have the following code in html:
<div class="return-form">
<div class="two_cols">
<div class="first_col">
<label for="namesinger">Name:</label> </div>
<div class="second_col">
<p id="name">Axl Rose</p>
</div>
</div>
I am using the PHP Simple HTML DOM Parser library and I would like to display only the name "Axl Rose" on the screen.
echo ($ name)
expected exit
Axl Rose
This how you can extract the data
<?php
// Load the HTML
$html = str_get_html('<div class="return-form">
<div class="two_cols">
<div class="first_col">
<label for="namesinger">Name:</label> </div>
<div class="second_col">
<p id="name">Axl Rose</p>
</div>
</di');
// Locate the date via div ID and display
echo $html->find('p[id=name]', 0)->plaintext;
?>
For more details Read this

Couldnt grab div element with a specified class name using simple_html_dom?

I am using simple_html_dom, i am having issues grabbing a div with a class name specified below is the code!
<?php
include 'simple_html_dom.php';
$html='
<div class="user-info ">
<div class="user-action-time">
answered <span title="2016-06-27 20:01:45Z" class="relativetime">Jun 27 at 20:01</span>
</div>
<div class="user-gravatar32">
<div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/09e3746cf7e47d4b3b15f5d871b91661?s=32&d=identicon&r=PG" alt="" width="32" height="32"></div>
</div>
<div class="user-details">
David Mulder
<div class="-flair">
';
echo $html->find('div[class=user-details]',0);
?>
What am i doing wrong here i am getting error Call to a member function find() on string in
Thanks!
You are tying to use Simple Html Dom to parse an html string.
Do not assign your html string to $html variable.
Assign it to an other, like $html_string.
Then use $html = str_get_html($html_string)
and
echo $html->find('div[class=user-details]',0);
You trying to call object method on a string variable. It should works:
$html = str_get_html('<div class="user-info ">
<div class="user-action-time">
answered <span title="2016-06-27 20:01:45Z" class="relativetime">Jun 27 at 20:01</span>
</div>
<div class="user-gravatar32">
<div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/09e3746cf7e47d4b3b15f5d871b91661?s=32&d=identicon&r=PG" alt="" width="32" height="32"></div>
</div>
<div class="user-details">
David Mulder
<div class="-flair">');

How do I extract keyword from webpage using PHP DOM

Here is a same of code I have extracted from a webpage...
<div class="user-details-narrow">
<div class="profileheadtitle">
<span class=" headline txtBlue size15">
Profession
</span>
</div>
<div class="profileheadcontent-narrow">
<span class="txtGrey size15">
administration
</span>
</div>
</div>
When displayed on the webpage it shows as "Profession administration". What I want to do is extract the profession, in this case "administration". However, it's not as simple as it might seem because this piece of code is repeated many times for various other questions, such as
<div class="user-details-narrow">
<div class="profileheadtitle">
<span class=" headline txtBlue size15">
Industry
</span>
</div>
<div class="profileheadcontent-narrow">
<span class="txtGrey size15">
banking
</span>
</div>
</div>
Any ideas on a good solution?
Please, do not use regular expressions for getting node values from a page.
PHP have a very nice class named DOMDocument. You can just fetch a page as DOMDocument:
$dom = new DOMDocument;
$dom->loadURL("http://test.de/page.html");
$finder = new DomXPath($doc);
$spaner = $finder->query("//*[contains(#class, 'size15')]");
echo $spaner->item(0)->nodeValue . "/" . $spaner->item(1)->nodeValue;

select children of the first element of a certain class using XPath

i have this type of code:
<div class="content">
<p></p>
<p></p>
<p></p>
</div>
<div class="content">
<p></p>
<p></p>
<p></p>
</div>
i wish to select all p elements from the first element with the class content.
i managed to select the first class by using:
(//div[#class="content"])[1]
but using (//div[#class="content"])[1]/p it still shows both classes
Here's an working example using PHP's SimpleXML. I've made some small changes to the HTML code you provided so the output would be more meaningful.
Regarding the XPath expression you provided I just removed the parenthesis and it all worked as expected.
NOTE: Following #LarsH's comment, I reverted the XPath expression as it was OK for starters. I took the liberty to update it based on its example.
<?php
$html = <<<HTML
<body>
<div class="content">
<p>1</p>
<p>2</p>
<p>3</p>
</div>
<div class="content">
<p>4</p>
<p>5</p>
<p>6</p>
</div>
<div>
<div class="content">
<p>7</p>
<p>8</p>
<p>9</p>
</div>
</div>
</body>
HTML;
$sxe = new SimpleXMLElement($html);
foreach ($sxe->xpath('(//div[#class="content"])[1]/p') as $p) {
echo "$p\n";
}
Output:
1
2
3
Link to codepad working example.

Categories