douxing2652 2014-02-05 17:05
浏览 32

使用Php爬行选项标记

I tried many things but i could not read the value on tag. I look similar examples here and on the different blogs but solution given there didnt solve my problem.

Html is like this on the site :

<div>
   <select name="ctl00$ContentPlaceHolder1$ddlCityID" id="ddlCityID">
    <option value="">Choose</option>
        <option value="14">Roma</option>
        <option value="15">Milan</option>
        <option value="16">Inter</option>
   </select>
</div>

My code on php :

$pageTitle = 'select[@id="ddlCityID"]';
$html = file_get_html($url);

  if ($html != null )
  {
     foreach($html->find($pageTitle) as $element) 
     {
    $link = $element->find('option');
    callfunction($link[0]->option);
     }
  }

Error :

Notice: Undefined offset: 0 in C:\Aptana Studio 3 = a.php on line 57
Notice: Trying to get property of non-object in C:\Aptana Studio 3 = a.php on line 57

Where is my problem and how can i get value of tag, and the text in the tag.

Thanks;

  • 写回答

1条回答 默认 最新

  • dow66098 2014-02-05 17:24
    关注

    Continue using simplehtmldom instead of str_pos (I would however suggest to change parser, since simplehtmldom is quite heavy).

    The solution you're seeking is this

    include('simple_html_dom.php');
    
    $str = '<div>
       <select name="ctl00$ContentPlaceHolder1$ddlCityID" id="ddlCityID">
        <option value="">Choose</option>
            <option value="14">Roma</option>
            <option value="15">Milan</option>
            <option value="16">Inter</option>
       </select>
    </div>';
    
    $pageTitle = 'select[id=ddlCityID]';
    $html = str_get_html($str);
    
    if ($html != null ) {
        foreach($html->find($pageTitle) as $element) {
            foreach($element->find('option') as $trueElement) {
                echo $trueElement->innertext;
            }
        }
    }
    

    Note that innertext will give you 'Choose', 'Roma', 'Milan', 'Inter'. trueElement now holds every OPTION tag.

    Also the '@' before 'id', is useless.

    UPDATE: This is the full working code I tested.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?