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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条