【问题标题】:Scrape data using html dom and class names使用 html dom 和类名抓取数据
【发布时间】:2014-04-29 06:22:30
【问题描述】:

我在这里尝试从网页中获取地址和电话号码。这是没有给出任何错误但也没有给出任何结果的代码。

有什么问题吗?

我正在尝试从页面中获取addressimagephone

代码如下:

include_once('simple_html_dom.php');
function getData($url)
{
    print("$url\n");
    $root = new stdClass();
    $items = array();
    $html = file_get_html($url);
    if($html) {
        $containers = $html->find('div.mapbox div.mapbox-text strong.street-address address.address');
        foreach($containers as $container) {
            $comments = $container->find('address.address span');
            $item = new stdClass();
            foreach($comments as $comment) {
                $address.= $comment->itemprop; //append the content of each span
            }
            echo $address;

            $getphone = $container->find('span.biz-phone');
            $phone = $getphone->itempro;
        }   

        $Imgcontainers = $html->find('div.js-photo photo photo-1 div.showcase-photo-box img.a la beverly sills');
        echo $Imgcontainers->img;
    }
}

$url = 'http://www.yelp.com/biz/locanda-san-francisco?start='.$i.'';
$root = getData($url);

【问题讨论】:

    标签: php dom


    【解决方案1】:

    <address> 标签中没有address 类,所以$containers 返回空。在您的 if 条件中使用以下代码

    $containers = $html->find('div.mapbox div.mapbox-text strong.street-address address');
    foreach($containers as $container) {
       $comments = $container->find('span');
       $item = new stdClass();
       foreach($comments as $comment) {
           $address.= $comment->itemprop; //append the content of each span
       }
       echo $address;
    
       $getphone = $container->find('span.biz-phone');
       $phone = $getphone->itempro;
    }
    $Imgcontainers = $html->find('div.js-photo photo photo-1 div.showcase-photo-box img.a la beverly sills');
    echo $Imgcontainers->img;
    

    【讨论】:

    • 谢谢老兄,但它给出了` streetAddressaddressLocalityaddressRegionpostalCode `而不是值。看看有什么问题
    • 使用$address.= $comment->plaintext;$address.= $comment->innertext; 这将返回文本。
    • @Manirbharathi:谢谢,它提供了明文地址。我在图像和电话中都使用了明文和内文,但这两者都没有打印出来
    猜你喜欢
    • 2018-05-23
    • 2021-05-18
    • 1970-01-01
    • 2015-07-25
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多