1

我知道这可能是我缺乏 PHP 经验,但我已经在同一件事上工作了 5 个小时。我受够了。

我能够返回我正在抓取的 10 个链接,但我似乎无法弄清楚如何变成一个数组。

我已经阅读了数百篇关于它是如何和不可能的帖子,并且 dom 对象无法传递到数组中。我尝试了很多不同的解决方法,但似乎没有任何效果。

我坚持不懈的主要原因是这篇文章: http ://martinsikora.com/parsing-html-pages-using-xpath 他似乎已经完成了它。

感觉我现在已经为此付出了太多的努力而放弃了。

还有另一种我完全想念的方法来完成这个吗?

<?php
    $curl = curl_init('http://www.notgoogle.com/search?q=Las+Vegas+Burgers');
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10' );

    $html = curl_exec($curl);
    curl_close($curl);

    if (!$html) {
        die( "something's wrong!" );
    }

    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $xpath = new DOMXPath($dom);

    $result = $xpath->query('//h3//a//@href');

    foreach($result as $node)
    {
        echo "{$node->nodeName} - {$node->nodeValue}";
    }

输出:

href - http://vegasmagazine.com/dining/articles/guide-the-very-best-burgers-in-townhref     - http://www.vegasburgerblog.com/href - http://www.yelp.com/c/las-vegas/burgershref - http://grindburgers.com/href - http://www.burger-bar.com/lv_main.phphref - http://www.e2hospitality.com/blt-burger-las-vegas/href - http://www.bachiburger.com/href - http://www.iloveburgers.com/href - http://www.stripburger.com/href - http://www.lbsburger.com/href - http://www.lasvegasweekly.com/news/2013/may/09/10-burgers-eat-right-now/href - http://www.urbanspoon.com/f/18/1708/Las-Vegas/Burger-Jointshref - http://www.gayot.com/restaurants/best-lasvegas-nv-top10-burgers_4lv.htmlhref - http://local.yahoo.com/NV/Las+Vegas/Food+Dining/Restaurants/Burgershref - http://govegas.about.com/od/dining/tp/bestburgers.htmhref - http://www.venetian.com/Las-Vegas-Restaurants/Casual-Dining/I-Love-Burgers/href

虽然我已经从随机文章/建议中改变了很多。基本上尝试了 PHP 手册中提到的所有 hack。虽然我是一个完全的初学者,所以我可能错过了一些东西

4

0 回答 0