I'm trying to extract some data from HTML DIV using Simple HTML Dom but it's not working right.
When I try to extract data from links/imgs it works fine but I have only a problem with DIVs.
HTML code:
<div class="price-container">
<div class="price-calc-container" id="attributes-calc-price">
<div class="current-price-container">
7,99 EUR
</div>
</div>
</div>
I want to extarct the number inside this div
$html = file_get_html("https://www.test.de/");
$preise = $html->find('div.current-price-container');
print_r($preise);
Sometime I will get an empty array sometimes nothing.
You have to start iterating over the find() result or use the last parameter to specify an index
$html = file_get_html("https://www.led-centrum.de/LED-Leuchtmittel/led-strahler/LED-Strahler-GU10/4058075160071.html");
$preise = $html->find('div.current-price-container', 0);
print_r($preise);
You can use a simple Jquery command like this
Try this
<div class="price-container">
<div class="price-calc-container" id="attributes-calc-price">
<div class="current-price-container">
7,99 EUR
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var x = $('.current-price-container').text();
console.log(x);
</script>
Related
Tag hierarchy in a webpage :
<body>
<div id='header'>
<h2>.....</h2>
</div>
<div id='main'>
<h2>...</h2>
//Some other content
<h2>...</h2>
</div>
<div id='footer'>
<h2>.....</h2>
</div>
</body>
[PROBLEM : ] From the above hierarchy structure of a webpaege, I want to extract only the <h2> tags which are inside the <div id='main'>. Can someone please please help me out ?
What I have tried is.... using HTML DOM of php $h2Tags = $htmlDom->getElementsByTagName('h2');, but this gives me all the <h2> tag which are outside of main div as well. Please guide me to a solution.
I have updated this to PHP:
h2_tags below will get list of h2s in main div:
$div_m = $htmlDom->getElementById('main');
$h2_tags = $div_m->getElementsByTagName('h2');
This is JS:
var div_m = document.getElementById("main");
var h2_tag = div_m.getElementsByTagName('h2');
<section id="123">
<div id="456">
some Data Here
</div>
</section>
I am a beginner to this and I need to copy data from section and div with their respective ids and use it some where else in my code. Please Help. Thanks for your time.
You can use
var htmlstring = $("#123").html();
.html() Function in jquery
Put your HTML in a php variable , and use it as you want
$div = '<section id="123">
<div id="456">
some Data Here
</div>
</section>';
I have this html code:
<div class="moviefilm">
SURSA 1
</div>
<div class="moviefilm">
SURSA 2
</div>
<div class="moviefilm">
SURSA 3
</div>
I want a php script to extract content of each and push him to php array to use with function
foreach ($divs as $div) {
//PHP CODE
}
you can do this with javascript getElementsByClassName then pass it to php by AJAX or something
I read this article - Get DIV content from external Website . I get source of website with file_get_contents() function and I have to extract from it content of two divs with same class name.
I have very similar problem, but with divs with same class name. E.g. I have code like that:
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
I want to get both content of both these divs. Solution accepted in article I linked support only one. My expected result is array like this:
$divs[0] = "Some conete"
$divs[1] = "Second Content"
Please give me advice what to do. I read about DOMDocument class, but have no idea how to use it.
i have used the simple html dom parser and your content can be extracted as
$html = file_get_html('your html file link');
$k=1;
foreach($html->find('div.baaa') as $e){
$divs[$k]=$e;
$k++;
}
echo $divs[1]."<br>";
echo $divs[2];
You could use XPath. XPath is a query language for XML. There are PHP functions that support Xpath.
For you the example could be:
File test.html:
<html>
<body>
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
</body>
</html>
The php code that extracts contents of divs with the class "baaa"
$xml = simplexml_load_file('test.html');
$data = $xml->xpath('//div[#class="baaa"]/text()');
foreach($data as $row) {
printf($row);
}
generates the following output:
Some conete
Second Content
Look for XPath tutorials if you need more complex searching or analyzing.
Try it with your data:
$file_contents = file_get_contents('http://address.com');
preg_match_all('/<div class=\"baaa\">(.*?)<\/div>/s',$file_contents,$matches);
print_r($matches);
BTW: Polska rządzi :)
<script type="text/javascript">
$(document).ready(function(){
$('.baaa').each(function(){
alert($(this).text());
});
});
</script>
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
I would like to append some html to a div when a button is clicked. The only thing that needs to be dynamic about it is that it needs to have a different id the the otherwise identical html above it. So if I have a structure like this:
<div id="container">
<div id="div1" class="inner">
<p id="P1">Some content</p>
</div>
<div id="div2" class="inner">
<p id="P2">Some content</p>
</div>
</div>
and to the end of container i would like to append
<div id="div3" class="inner">
<p id="P3">Some content</p>
</div>
What would be a good method to store this HTML? Keep it all in a php page and post the number the new div's id should be incremented to? Or is is smarter to put the html inside a string, and have a regular expression increment all of the numbers. Or is there some incredibly obvious way to do this that I've completely missed.
Jquery is an option, as that's already on the page.
Also, if this question seems too open-ended, please let me know in the comments how I can change it before closing it.
Thank you very much.
Based on your answers to comments so far (i.e. that the id's aren't really important, and that you will have input elements in the content), I would say that you would want to redo your html to look like this:
<div id="container">
<div class="inner">
<p><input type="text" name="order_item[]" value="" /></p>
</div>
<div class="inner">
<p><input type="text" name="order_item[]" value="" /></p>
</div>
</div>
Note there are no longer id's and I have used array notation for your input elements such that they will be posted as an array to the receiving script (eliminating the need to increment counters in javascript or parse different posted variable names into a usable array in PHP on the server).
And then use something like this in jQuery
$('#button_id').click(function() {
$('#container > div.inner:last').clone().val('').appendTo('#container');
});
Put the HTML you want to add each time into a hidden div, then use jQuery to copy and add that to the relevant location. For instance:
<div id="appendContents" style="display:none;">
<div class="inner">
<p id="P3">Some content</p>
</div>
</div>
then jquery:
var shownDivs = <?php echo $numberOnScreenFromBeginning;?>;
$('#theButtonYouWantClickable').click(function()
{
var newContent = $('#appendContents').clone();
newContent.$('div.inner').get(0).id = 'div'+shownDivs;
newContent.$('div.inner p').get(0).id = 'P'+shownDivs;
// Where does P content come from??
shownDivs++;
$('#container').append(newContent);
});
Try this Please: Working Demo http://jsfiddle.net/eNXmr/
In the code below it will calculate the lenght of the div hence dynamically set the id as 1, 2, 3, and beyond.
Hope it fits you need :)
API used: http://api.jquery.com/append/
Code
$('#hulk').click(function() {
alert("Total div inside ==> " + $('#container').find('.inner').length);
var next_num = parseInt($('#container').find('.inner').length) + 1;
$('#container').append(' <div id="div' + next_num + '" class="inner"><p id="P' + next_num + '">Some content</p></div>');
});
HTML
<div id="container">
<div id="div1" class="inner">
<p id="P1">Some content</p>
</div>
<div id="div2" class="inner">
<p id="P2">Some content</p>
</div>
</div>
<input type="button" value="Click hulk" id="hulk" />