php - 那么,我想抓取一个网页吗?

标签 php javascript ajax

Possible Duplicates:
How to write a crawler?
Best methods to parse HTML

我一直想知道如何做这样的事情。我不是该网站 ( http://poolga.com/ ) 的所有者/管理员/网站管理员,但我希望获取的信息是公开的。此页面 ( http://poolga.com/artists ) 是所有为该网站做出贡献的艺术家的目录。但是,此页面上的链接会转到包含此 anchor 标记的另一个页面,该 anchor 标记包含指向艺术家实际网站的链接。

<a id="author-url" class="helv" target="_blank" href="http://aaaghr.com/">http://aaaghr.com/</a>

我讨厌必须命令并单击目录中的链接,然后单击艺术家网站的链接。我希望有一种方法可以让一批 10 个艺术家网站链接在浏览器中显示为选项卡,仅供临时查看。然而,将这些 href 放入某种数组本身就是一项壮举。任何编程语言中的任何想法或方向/谷歌搜索都很棒!这甚至可以被称为“爬行”吗?感谢您的阅读!

更新

我在本地 php MAMP 服务器上使用了 Simple HTML DOM 和这个脚本,花了一点时间!

$artistPages = array();
foreach(file_get_html('http://poolga.com/artists')->find('div#artists ol li a') as $element){
  array_push($artistPages,$element->href);
}

for ($counter = 0; $counter <= sizeof($artistPages)-1; $counter += 1) {
    foreach(file_get_html($artistPages[$counter])->find('a#author-url') as $element){
           echo $element->href . '<br>';
    }
}

最佳答案

我最喜欢的用于在 dom 中导航的 php 库是 Simple HTML DOM .

set_time_limit(0);
$poolga = file_get_html('http://poolga.com/artists');
$inRefs = $poolga->find('div#artists ol li a');
$links = array();

foreach ($inRefs as $ref) {
    $site = file_get_html($ref->href);
    $links[] = $site->find('a#author-url', 0)->href;
}

print_r($links);

我认为代码是非常不言自明的。

编辑:有一个拼写错误。考虑到有如此多的链接,脚本需要非常非常长的时间才能完成;这就是为什么我使用 set_time_limit() 。去做其他事情并让脚本运行。

关于php - 那么,我想抓取一个网页吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5734496/

相关文章:

javascript - "new Date(d)"和 "new Date(+d)"之间的区别

java - 数据源拒绝建立连接java

javascript - 将数组从页面模板传递到 WordPress 主题文件夹中的另一个 php 文件

php - mysql - 从父表插入id

JavaScript 对象数组未正确排序

javascript - 广泛的设计实现 : PHP, MySQL, knockout

php - 使用 Jquery/ajax 将数据发送到数据库

PHP:周从星期一开始,但星期日的 "monday this week"会在下周成为星期一

php - 动态调用类中的方法

php - 删除 echo json_encode() 上的引号;