【问题标题】:Check if page url exists (fast way - 1mio sites)检查页面 url 是否存在(快速方式 - 1mio 站点)
【发布时间】:2013-05-20 20:28:47
【问题描述】:

我有一个 Alexa 排名前 100 万的列表。我想检查这 100 万个站点中哪些是具有页面 www.domain.com/pageNameUrl 的站点。 我试过了

foreach($sites as $site){    
  $file_headers = @get_headers($site);
  if(strpos($file_headers[0],"200 OK") !== false) {
    $exists = true;
    //save site name code...
  } else {
    $exists = false;
  }
}

但是这段代码花费了太多时间。浏览所有站点需要 1 个月甚至更长时间。还有其他更快的方法吗?

【问题讨论】:

  • 您运行它的服务器的配置是什么?也许你应该考虑在云中晒太阳。如果您在自己的机器上运行它,很明显脚本运行缓慢。
  • 你真的需要考虑使用另一种能够线程化的语言。 PHP在这里不是很好用。您可以通过并行执行大量检查来提高速度
  • 似乎适合异步请求
  • 考虑使用 Node.js 异步执行此操作

标签: php parsing file-get-contents


【解决方案1】:

我认为 php 不是该工作的合适人选。您可能会考虑像 nodeJs 这样非常适合异步作业的东西。看看这个(例子取自https://npmjs.org/package/crawler

var Crawler = require("crawler").Crawler;

var c = new Crawler({
    // here you can define, how many pages you want to do in parallel
    "maxConnections":10,

    // This will be called for each crawled page
    "callback":function(error,result,$) {
        // mark this page as available or not based on the reponse
        console.log(result.statusCode);
    }
});

// Queue all your urls in a loop, they all will be push asynchronously to the crawler job
c.queue("http://www.google.de");
c.queue("http://www.amazon.de");
c.queue("http://www.facebook.de");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多