duanjiuhong5843 2013-05-08 13:44
浏览 49
已采纳

从api代码获取特定标签

I want to get the IMG SRC tag from the following code:

<pod title="Scientific name" scanner="Data" id="ScientificName:SpeciesData" position="200" error="false" numsubpods="1">
<subpod title="">
<plaintext>Canis lupus familiaris</plaintext>
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17941cd1c5fi21h72ac2000057i1ae7gc4986gdf?MSPStoreType=image/gif&s=44" alt="Canis lupus familiaris" title="Canis lupus familiaris" width="139" height="18"/>
</subpod>
</pod>

I know how to get the plaintext information but how do I get the img src information? Here's what I have to get the plaintext information:

<?php
if(isset($_POST['q'])){
include 'WolframAlphaEngine.php';
$engine = new WolframAlphaEngine( 'APP-ID' );

$resp = $engine->getResults("$q");

$pod = $resp->getPods();

$pod1 = $pod[1];


foreach($pod1->getSubpods() as $subpod){
  if($subpod->plaintext){
    $plaintext = $subpod->plaintext;
    break;
  }
}

$result = substr($plaintext, 0,strlen($plaintext)-3);

echo "$plaintext";

}
?>

It's not a duplicate of Grabbing the href attribute of an A element because I can't use DOM on my Godaddy hosting. I've tried it before.

  • 写回答

1条回答 默认 最新

  • dongle0396 2013-05-08 14:16
    关注

    I've never worked with that WolframAlphaEngine before. I assume it's this one:

    If so, have a look at https://github.com/brkeerthi/chethana/blob/master/include/WASubpod.php

    class WASubpod {
      // define the sections of a response
      public $attributes = array();
      public $plaintext = '';
      public $image = '';
      public $minput = '';
      public $moutput = '';
      public $mathml = '';
    

    As you can see, it has properties not only for plaintext, but also for attributes and other stuff. It might be as easy as

    foreach ($pod1->getSubpods() as $subpod) {
        $attributes = $subpod->attributes;
        if (isset($attributes['src']) {
            echo $attributes['src'];
        }
    }
    

    But since the documentation of that library is non-existent, I cannot tell. So if this doesn't work, just var_dump the $subpod to see what it contains.

    Likewise, have a look at the other classes to find out what they do:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记