Use simple dom to get select by name

381 views Asked by At

I'm trying to use simple dom to get an element by name

It's to crawl an old website and they only use name

<select name="id_items_1" required="">
    <option value="">Seleccione su talla</option>
    <option value="231085" data-talla="36" data-stock="3">36</option>
    <option value="231086" data-talla="37" data-stock="1">37</option>
</select>

I tried this but it returns null:

include('simple_html_dom.php');
$html = str_get_html($url);
$elem = $html->find('div[name=id_items_1]', 0);
var_dump($elem);

Is there any way to do this with simple dom?

2

There are 2 answers

4
Phu Duy On BEST ANSWER

you have to change to file_get_html and add http:// to url

include('simple_html_dom.php'); 
$url="http://platanitos.com/Platanitos-ZC-LUCY-161-Negro-49272";; 
$html = file_get_html($url); 
$elem = $html->find('select[name=id_items_1]', 0)->innertext; 
var_dump($elem);
1
Marko Krstic On

you need to call file_get_contents first in order to get html of remote url and then use select[name=id_items_1] option to find your selector:

include('simple_html_dom.php');
$html = str_get_html(file_get_contents($url));
$elem = $html->find('select[name=id_items_1] option', 0);
var_dump($elem);