dousi8237 2010-09-30 07:18
浏览 42
已采纳

如何编写preg_match_all只是为了获取一个特定的元素?

Until the website give me an access to his API, i need to display only 2 things from this website :

What i want to grab // Example on a live page

Those 2 things are contained in a div :

<div style="float: right; margin: 10px;">
here what i want to display on my website
</div>

The problem is that i found an example on stackoverflow, but i never wrote preg_match before. How to do this with the data i want to grabb ? Thank you

<?php   $html = file_get_contents($st_player_cv->getUrlEsl());

preg_match_all(
    'What do i need to write here ?',
    $html,
    $posts, // will contain the data
    PREG_SET_ORDER // formats data into an array of posts
);

foreach ($posts as $post) {
    $premium = $post[1];
    $level = $post[2];

    // do something with data
}
  • 写回答

3条回答 默认 最新

  • duanchao1002 2010-09-30 07:42
    关注

    The DOM way to do it would be

    libxml_use_internal_errors(TRUE);
    $dom = new DOMDocument;
    $dom->loadHTMLFile('http://www.esl.eu/fr/player/5178309/');
    libxml_clear_errors();
    
    $xPath = new DOMXPath($dom);
    $nodes = $xPath->query('//div[@style="float: right; margin: 10px;"]');
    foreach($nodes as $node) {
        echo $node->nodeValue, PHP_EOL;
    }
    

    but there is a whole slew of JavaScript in the page that modifies the DOM heavily after the page was loaded. Since any PHP script based fetching will not execute any JavaScript, the style we search for in the XPath does not exist yet and we won't get any results (the Regex suggesed by Hannes doesn't work for the same reason). Neither do the level numbers on the badge exist yet.

    As Wrikken pointed out in the comments, there also seems to be some mechanism to block certain requests. I had the message once, but I am not sure what triggers it, because I could also fetch page on several occasions.

    To cut a long story short: you cannot achieve what you are trying to do with this page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 某东JD算法逆向算法
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部