我正在尝试获取多个产品的一些细节,以下代码适用于单个网址,并且工作正常: -
<?php
$url = "http://www.flipkart.com/healthgenie-hd-221-digital-black-dotted-weighing-scale/p/itmeatqzeehkdsmg";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($output);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
// Product Name
$find_product_name = $xpath->query('//h1[@class="title"]');
if($find_product_name->length > 0)
{
$product_name= $find_product_name->item(0)->nodeValue;
}
else
{
$product_name="Product Name Not Found";
}
// Sold by
$find_seller = $xpath->query('//a[@class="seller-name"]');
if($find_seller->length > 0)
{
$seller= $find_seller->item(0)->nodeValue;
}
else
{
$seller="Seller Not Found";
}
// List Price
$find_list_price = $xpath->query('//span[@class="price"]');
if($find_list_price->length > 0)
{
$list_price= $find_list_price->item(0)->nodeValue;
}
else
{
$list_price="Not Available";
}
// Sale Price
$find_sale_price = $xpath->query('//span[@class="selling-price omniture-field"]');
if($find_sale_price->length > 0)
{
$sale_price= $find_sale_price->item(0)->nodeValue;
}
else
{
$sale_price="Not Available";
}
// Stock Status
$find_stock = $xpath->query('//div[@class="out-of-stock-status"]');
if($find_stock->length > 0)
{
$stock= $find_stock->item(0)->nodeValue;
}
else
{
$stock = "In Stock!";
}
//
?>
<table width="100%" align="center" border="1">
<tr>
<th>Product Name</th>
<th>Sold By</th>
<th>List Price</th>
<th>Sale Price</th>
<th>Stock Status</th>
</tr>
<tr>
<td><?php echo '<a href="'.$url.'" target="_blank">'.$product_name.'</a>'; ?></td>
<td><?php echo $seller; ?></td>
<td><?php echo $list_price; ?></td>
<td><?php echo $sale_price; ?></td>
<td><?php echo $stock; ?></td>
</tr>
</table>
现在我需要一种方法来同时从多个URL中获取数据。我希望在数组中使用URL的多个URL具有相同的过程。例如: -
<?php
$url = array(
"http://www.flipkart.com/healthgenie-hd-221-digital-black-dotted-weighing-scale/p/itmeatqzeehkdsmg",
"http://www.flipkart.com/healthgenie-hd-221-digital-black-dotted-weighing-scale/p/itmeatqzeehkdsmg",
"http://www.flipkart.com/healthgenie-hd-221-digital-black-dotted-weighing-scale/p/itmeatqzeehkdsmg"
);
?>
我请求所有开发人员请看一下并帮助我。提前谢谢了。
答案 0 :(得分:0)
将网址设为CSV并创建上传表单。
<?php
$fsn = fopen($_FILES['fsn']['tmp_name'], 'r');
if(!$fsn)
{
die('Cannot open uploaded file.');
}
while (($data = fgetcsv($fsn)) !== FALSE)
{
$urls[] = $data[0];
}
foreach($urls as $url)
{
// Execute the code
}
?>