V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
15
V2EX  ›  PHP

php 中 curl 配合 simple_html_dom 进行页面数据抓取的问题

  •  
  •   15 · 2015-09-30 09:58:49 +08:00 · 3422 次点击
    这是一个创建于 3133 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有一个页面需要登录才能读取,我已经用 crul 模拟登录成功获取了页面内容

    页面的数据是表格,我想用 simple_html_dom 来分析 获取需要的数据

    但是 curl 使用的 curl_exec
    simple_html_dom 使用的是 file_get_html

    curl_exec 获取到的数据,没办法直接在 simple_html_dom 中使用啊

    比如 $content = file_get_html(网址)

    foreach($content->find('a') as $row)
    echo $row;

    这是正常的
    -----------------------------------------------------------
    但是 $content = curl_exec(网址)

    foreach($content->find('a') as $row)
    echo $row;

    这是没有内容的

    怎么办?
    13 条回复    2015-10-01 00:54:29 +08:00
    Paranoid
        1
    Paranoid  
       2015-09-30 10:05:44 +08:00   ❤️ 1
    str_get_html
    haiyang416
        2
    haiyang416  
       2015-09-30 10:05:56 +08:00
    str_get_html
    towser
        3
    towser  
       2015-09-30 10:07:18 +08:00   ❤️ 1
    $content = file_get_html(curl_exec(url));

    有用的话记得给我打钱。
    towser
        4
    towser  
       2015-09-30 10:08:17 +08:00   ❤️ 2
    $content = str_get_html(curl_exec(url));

    手快了,但打钱的事你别忘了。
    eoo
        5
    eoo  
       2015-09-30 10:10:32 +08:00 via Android   ❤️ 1
    $content = curl_exec

    试过$html->load($content); 没?
    wjfz
        6
    wjfz  
       2015-09-30 10:21:42 +08:00   ❤️ 1
    https://github.com/samacs/simple_html_dom/blob/master/simple_html_dom.php

    看源码可知, file_get_html 返回了一个 simple_html_dom 对象,对象下有 find 方法。
    然而 curl_exec 并不是一个对象,他下面没有 find 方法,所以你不能用。


    file_get_html 函数下的处理是$contents = file_get_contents($url, $use_include_path, $context, $offset); 所以这里必须传 URL 。
    str_get_html 函数下的处理是 直接对字符串进行处理。


    记得给 4 楼打钱。
    15
        7
    15  
    OP
       2015-09-30 10:29:51 +08:00
    str_get_html ,正解!
    多谢大家

    $html->load($content);
    没试成功。。
    ruchee
        8
    ruchee  
       2015-09-30 10:33:12 +08:00
    是妹子就不用打钱了吧, 4 楼
    LeoQ
        9
    LeoQ  
       2015-09-30 10:34:50 +08:00 via Android
    打钱了么,哈哈哈哈哈
    towser
        10
    towser  
       2015-09-30 11:18:16 +08:00
    @ruchee 不管是妹是姐,多少也得打点。现在经济危机,就是地主家也没有余粮。
    mactaew
        11
    mactaew  
       2015-09-30 13:18:42 +08:00 via iPhone
    学艺不精,已经放弃用 php 处理 html dom 。 node 的 cheerio 不错!
    zonghua
        12
    zonghua  
       2015-09-30 23:20:28 +08:00
    @towser PHP 还真是面向对象,竟然有这么多函数。我之前还一直用正则匹配出来。
    towser
        13
    towser  
       2015-10-01 00:54:29 +08:00
    @zonghua 是的, PHP 5 之后已经逐步完善面向对象了。 dom 解析配合正则提取做爬虫也是呱呱叫。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1891 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:25 · PVG 00:25 · LAX 09:25 · JFK 12:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.