【发布时间】:2017-08-13 22:28:04
【问题描述】:
我正在尝试解析网站 robots.txt 文件(其中 facebook 将是一个随机 url)。
我想删除任何不用于用户代理的行(例如本例中的前两行)。所以也许摆脱任何不以 A、D 或 U 开头的行?
我还想让每个用户代理都有自己的关联数组,标题是用户代理,即允许所有谷歌机器人允许和禁止 url 我将在 $arr[googleBot] 上打印_r。
这是我目前的代码!
<?php
//URl to start crawling
$start = "https://www.facebook.com";
//Url to crawl, crawled or not crawl
$crawling = array();
$crawled = array();
$disallow = array();
function getRobots($url)
{
$robotsUrl = $url . "/robots.txt";
ini_set("user_agent","Agent (https://www.useragent.com)");
$robots = @file_get_contents($robotsUrl);
$robots = explode("\n", $robots);
$robots = preg_grep('/[^\s]/', $robots);
print_r($robots);
}
$result = getRobots($start);
【问题讨论】:
-
那你被什么困住了?
标签: php regex web-scraping web-crawler robots.txt