简体   繁体   中英

Crawling option tag with 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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM