0

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?

Saikios
  • 3,623
  • 7
  • 37
  • 51
  • you have to add attribute when find element. Example: `$elem = $html->find('div[name=id_items_1]', 0)->plaintext;` – Phu Duy Jul 19 '16 at 03:40
  • @PhuDuy it didn't work, still null include('simple_html_dom.php'); $url="http://platanitos.com/Platanitos-ZC-LUCY-161-Negro-49272"; $html = str_get_html($url); $elem = $html->find('select[name=id_items_1]', 0)->plaintext; var_dump($elem); – Saikios Jul 19 '16 at 03:47
  • it should be `file_get_html($url)` – Kevin Jul 19 '16 at 03:52
  • @Ghost thanks that worked but it's only retrieving the first result of the select and it has a few more options – Saikios Jul 19 '16 at 03:58

2 Answers2

1

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);
Marko Krstic
  • 1,417
  • 1
  • 11
  • 13
1

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);
Phu Duy
  • 187
  • 1
  • 8