php 中 curl 配合 simple_html_dom 进行页面数据抓取的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
15
V2EX    PHP

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

  •  
  •   15 2015-09-30 09:58:49 +08:00 3835 次点击
    这是一个创建于 3670 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有一个页面需要登录才能读取,我已经用 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, $useinclude_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     Solana     1119 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 18:11 PVG 02:11 LAX 11:11 JFK 14:11
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86