【发布时间】:2011-06-19 13:56:16
【问题描述】:
我有一个 PHP 抓取脚本,用于抓取我网站上的页面。然后该脚本将内容解析为 HTML 并将其输出给用户。我偶然发现在 PHP 中使用 useragent 函数来假装你是一个爬虫,例如 GoogleBot。如何将我的两个脚本组合在一起,以便我正在抓取的页面认为我是爬虫?
我的爬虫 PHP 代码是:
$query=$_REQUEST['q'];
$html = file_get_contents("search.php?q=$query");
preg_match_all(
'/<div class="cl1 cld">.*?<a rel="nofollow" class="l le" href="(.*?)">(.*?)<\/a>.*?<div class="cra">(.*?)<\/div>.*?<div class="clud">(.*?)<\/div>.*?<\/div>/s',
$html,
$posts, // will contain the blog posts
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($posts as $post) {
$link = $post[1];
$title = $post[2];
$description = $post[3];
$url = $post[4];
echo "<div class='result'><div class='title'><a href='$link'>$title</a></div>$description<div class='url'>$url</div></div>";
}
?>
我有这行伪装成爬虫的代码。
$userAgent = 'MyScraperBot (http://www.mysite.com/)';
【问题讨论】:
标签: php