duanhuiqing9528 2012-01-08 17:38
浏览 44
已采纳

检索包含特定span类的数据

using file_get_contents, I open an Internet URL and get the contents of this webpage.

Inside the HTML there are many identical span class tags:

<span class="always-the-same-class">always dynamic text</span>

Now, I want to get an array containing all the "dynamic text" contained in any of this tags. It is not necessary to eliminate duplicated entries (I need them).

Is this possible? How could I do?

  • 写回答

3条回答 默认 最新

  • dsbiw2911188 2012-01-08 17:54
    关注

    If I understood correctly, this has to be PHP as it is on the server, not in the browser. So I'd do something like

    $html=file_get_contents(HTML_URL);
    $a=preg_match_all("/\<span class\=\"always-the-same-class\"\>(.*?)\<\/span\>/",$html,$b);
    echo $a;
    print_r($b[1]);
    

    $a has hit count, $b[1] the hits

    Tested this against

    <html>
    .. blah ..
    <body>
    
    .. blah ..
    
    <span class="always-the-same-class">always dynamic text A</span>
    <span class="always-the-same-class">always dynamic text B</span>
    <span class="always-the-same-class">always dynamic text C</span>
    
    .. blah ..
    
    </body>
    </html>
    

    and output was

    3
    Array
    (
        [0] => always dynamic text A
        [1] => always dynamic text B
        [2] => always dynamic text C
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭