首页 文章

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

提问于
浏览
-2

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

这是我获取所有链接的代码

<?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 回答

  • 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列表进行排名 . 一种方法是使用 Map /字典,其中url是索引,相关性等级是值 .

    祝好运!

相关问题