【问题标题】:How to make this crawler more efficient [closed]如何让这个爬虫更高效[关闭]
【发布时间】:2017-06-20 16:40:42
【问题描述】:

我构建了这个网络爬虫。

https://github.com/shoutweb/WebsiteCrawlerEmailExtractor

//Regular expression function that scans individual pages for emails
    function get_emails_from_webpage($url)
    {
      $text=file_get_contents($url);
      $res = preg_match_all("/[a-z0-9]+[_a-z0-9\.-]*[a-z0-9]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})/i",$text,$matches);
      if ($res) {
          return array_unique($matches[0]);
      }
      else{
          return null;
      }
    }

//URL Array
$URLArray = array();

//Inputted URL right now it just pulls it from a GET variable but you can do alter this any way you want
$inputtedURL = $_GET['url'];


//Crawling the inputted domain to get the URLS
$urlContent = file_get_contents("http://".urldecode($inputtedURL));
$dom = new DOMDocument();
@$dom->loadHTML($urlContent);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

$scrapedEmails = array();

for($i = 0; $i < $hrefs->length; $i++){
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    $url = filter_var($url, FILTER_SANITIZE_URL);
    //array_push($scrapedEmails, $hrefs->length);
    // validate url
    if(!filter_var($url, FILTER_VALIDATE_URL) === false){
        if (strpos($url, $inputtedURL) !== false) {
                array_push($URLArray, $url);
            }

    }
}

//Extracting the emails from URLS that were crawled
foreach ($URLArray as $key => $url) {
    $emails = get_emails_from_webpage($url);

    if($emails != null){
      foreach($emails as $email) {
          if(!in_array($email, $scrapedEmails)){
            array_push($scrapedEmails,$email);
        }
      }
    } 
}


//Ouputting the scraped emails in addition to the the number of URLS crawled
foreach($scrapedEmails as $value) {
    echo $value . " " . count($URLArray);
}

它基本上会转到您输入的域,获取所有页面,然后检查是否有电子邮件。

每个域最多可能需要 30 秒才能抓取。我想看看是否有办法加速这个网络爬虫。我想的一种方法是将其限制为仅联系页面,但我想不出一个聪明的方法来做到这一点。

【问题讨论】:

  • 出于好奇——为什么不能执行爬虫的多个副本,而不是一次执行一个域?
  • 啊,这很聪明……我该怎么做,我也不知道为什么这会被否决。这不是一个合理的问题吗?
  • 所以,让我直说吧。您要求我们为您提供一种更好的方式来收集您将用来做的电子邮件地址……什么?向他们发送垃圾邮件?卖给发送垃圾邮件的人?你看到我对你的问题的问题,对吧?我觉得在这里问这个很无耻。
  • 所以你的论点是“有些人也不要脸,所以我可以不要脸”。好一个。

标签: php performance web-scraping web-crawler


【解决方案1】:

只要你的意图不是邪恶的--

如评论中所述,实现此目的的一种方法是并行执行爬虫(多线程)——而不是一次只执行一个域。

类似:

exec('php crawler.php > /dev/null 2>&1 &');
exec('php crawler.php > /dev/null 2>&1 &');
exec('php crawler.php > /dev/null 2>&1 &');
exec('php crawler.php > /dev/null 2>&1 &');
exec('php crawler.php > /dev/null 2>&1 &');

在服务器上,您可以设置一个CRON 作业,它将自动执行此操作,这样您就不必手动运行它。

【讨论】:

  • 谢谢 - 我正在使用它来更好地了解我们现有客户的资料,我们有超过 50,000 个网站可供抓取
猜你喜欢
  • 1970-01-01
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
相关资源
最近更新 更多