Reputation: 256
I am working on a web scraper. I have searched the product title on a webpage with my product.if same product exist on the page then i want to extract the price of that product. for this i am using XPath
here is my html code from which i need to extract price.
<div class="products_list_table">
<table id="products_list_table_table" cellspacing="6" cellpadding="0" border="0">
<tbody>
<tr>
<td valign="top" align="center">
<span class="product_title">Malik Candy FC Composite Hockey Stick</span>
<div class="list_price_bar all-cnrs">
<span class="list_price_title">Price Now:</span>
<span class="list_sale_price">£40.00</span>
</div>
</td>
</tr>
<tr>
<td valign="top" align="center">
<span class="product_title">Malik TC Stylish Hockey Stick</span>
<div class="list_price_bar all-cnrs">
<span class="list_price_title">Price Now:</span>
<span class="list_sale_price">£70.00</span>
</div>
</td>
</tr>
...
</tbody>
</table>
<div>
There are many tr tags for all products and i search for a product title if it found i want to extract price of that product.
here is my php code in file test.php
<?php
set_time_limit(0);
if(isset($_POST['title']) && $_POST['title']!= ''){
$product_title = mysql_real_escape_string($_POST['title']);
$url = 'http://www.example.com';
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$found = $xpath->evaluate("boolean(//span[contains(text(), '". $product_title ."' )])");
if($found == false){
echo "Not Found";
}
else {
$elements = $xpath->evaluate("//span[@class='list_sale_price']");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue.'<br>';
}
}
}
}
}
?>
here i am using form in test.php to search product
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<label>Enter product title to search</label><br /><br />
<input type="text" name="title" size="50" /><br /><br />
<input type="submit" value="Search" onclick="msg()"/>
</form>
</body>
</html>
After finding the product, i want to extract price of that product but the it displays all the prices on the page. where i made mistake. Need xpath expression to extract the price of matched product.
Upvotes: 2
Views: 621
Reputation: 23637
You don't need multiple expressions. You can extract the price with one XPath expression by selecting the div
following your matched span
, and in this context, extracting its child span
which has the class of list_sale_price
:
//span[contains(text(), 'Malik Candy' )]/following-sibling::div/span[@class='list_sale_price']
Upvotes: 1