使用PHP进行网站爬取 [英] Website Scraping Using PHP

查看:70
本文介绍了使用PHP进行网站爬取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php代码可以提取此网站中的产品类别: http://www.tradeindia.com/ 。到目前为止,我仅提取了类别。我如何使它也可以提取其旁边的产品编号,因为它没有任何类名?

I have a php code that could extract the product categories in this website: http://www.tradeindia.com/. So far I had managed to extract only the categories. How do I make it so that it will also extract the product numbers beside it since its not in any class name?

我的代码:

<?php 
//header('Content-Type: text/html; charset=utf-8'); 
$grep = new DoMDocument(); 
@$grep->loadHTMLFile("http://www.tradeindia.com/"); 
$finder = new DomXPath($grep); 
$class = "cate_menu"; 
$nodes = $finder->query("//*[contains(@class, '$class')]"); 

$total_L = 0; 
foreach ($nodes as $node) { 
$span = $node->childNodes; 
echo '<br>' . $span->item(0)->nodeValue . ' : '; 
} 

?> 

来自网站的源代码:

<td align="left" style="padding-left:8px;color:blue"><a href=/Seller/Agriculture/ class="cate_menu" >Agriculture</a>(100892)</td>
<td align="left" style="padding-left:8px;color:blue"><a href=/Seller/Apparel-Fashion/ class="cate_menu" >Apparel & Fashion</a>(237902)</td>
<td align="left" style="padding-left:8px;color:blue"><a href=/Seller/Automobile/ class="cate_menu" >Automobile</a>(78614)</td>

我需要括号之间的数字。

I need the numbers between brackets.

推荐答案

我不是xpath专家,但是我要做的是首先使用该指针类别来定位该特定表,然后从那里获取那些行并开始循环查找行。

I'm not an xpath guru, but what I would do is to target first that particular table using that needle categories, then from there get those rows based on that and start looping on found rows.

粗略示例:

$grep = new DOMDocument();
@$grep->loadHTMLFile("http://www.tradeindia.com/");
$finder = new DOMXpath($grep);

$products = array();
$nodes = $finder->query("
    //td[@class='showroom1'][contains(text(), 'CATEGORIES')]
    /parent::tr/parent::table/parent::td/parent::tr
    /following-sibling::tr
    /td[1]/table/tr/td/table/tr
");

if($nodes->length > 0) {
    foreach($nodes as $tr) {
        if($finder->evaluate('count(./td/a)', $tr) > 0) {
            foreach($finder->query('./td/a[@class="cate_menu"]', $tr) as $row) {
                $text = $row->nodeValue;
                $number = $finder->query('./following-sibling::text()', $row)->item(0)->nodeValue;
                $products[] = "$text $number";
            }

        }
    }
}

echo '<pre>';
print_r($products);

示例输出

这篇关于使用PHP进行网站爬取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆