如何抓取网页并在所有网页上搜索特定文字?

时间:2014-05-14 13:30:16

标签: web-crawler

我正在使用简单的HTML DOM库处理网络抓取工具。我已经获得了网站的所有链接。现在我想抓取我获得的所有链接/页面,在所有页面上搜索并查找一些特定文本。

这是获取所有链接的代码

<?php

include_once("simple_html_dom.php");
set_time_limit(0);
$path='http://www.barringtonsports.com';
$html = file_get_contents($path);
$dom = new DOMDocument();
@$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++ ) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
$nurl = $path.$url.'<br>';
echo $nurl; 
}


?>

1 个答案:

答案 0 :(得分:1)

以下是一些伪代码:

Create a list of $interestingURLs
Create a list of $matchingURLs
Call Search with your root site, Search("barringtonsports.com")
Search($site):
    1: append $site to $interestingURLs
    2: $searchedSites = 0
    3: for each ($entry in $interestingURLs):
        3a: SearchForSite($entry)
        3b: $searchedSites++
        3c: if ($searchedSites > MAX_SEARCHES):
            3c1: for each ($site in $matchingURLs) print $site

SearchForSite($site):
    1: load dom for $site
    2: search dom for interesting content, if exists - add $site to $matchingURLs
    3: extract all links
    4: for each link append to the end of $interestingURLs

下一步的业务是将machingURL列表相关联。一种方法是使用地图/字典,其中网址是索引,相关性等级是值。

祝你好运!

相关问题