【问题标题】:How to crawl website pages and search for specific text on all pages?如何爬取网站页面并在所有页面上搜索特定文本?
【发布时间】:2014-05-14 13:30:15
【问题描述】:

我正在使用简单的 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; 
}


?>

【问题讨论】:

    标签: web-crawler


    【解决方案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 列表的相关性进行排名。一种方法是使用地图/字典,其中 url 是索引,相关性排名是值。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      相关资源
      最近更新 更多