【问题标题】:Use simple dom to get select by name使用简单的 dom 按名称获取选择
【发布时间】:2016-07-19 12:27:11
【问题描述】:

我正在尝试使用简单的 dom 按名称获取元素

这是抓取一个旧网站,他们只使用名称

<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>

我试过了,但它返回 null:

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

有没有办法用简单的 dom 做到这一点?

【问题讨论】:

  • 你必须在查找元素时添加属性。示例:$elem = $html-&gt;find('div[name=id_items_1]', 0)-&gt;plaintext;
  • @PhuDuy 它没有用,仍然为空 include('simple_html_dom.php'); $url="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);
  • 应该是file_get_html($url)
  • @Ghost 感谢它的工作,但它只检索选择的第一个结果,它还有更多选项

标签: php html simpledom


【解决方案1】:

您必须更改为 file_get_html 并将 http:// 添加到 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);

【讨论】:

  • 那是 awsemoe,但它只给了我第一个选项,它对你也一样吗?
  • 你想要什么结果?
  • 会喜欢这个 或 value="231085" data-talla="36" data-stock ="3"
  • plaintext更改为innertext
【解决方案2】:

您需要先调用file_get_contents 以获取远程url 的html,然后使用select[name=id_items_1] option 找到您的选择器:

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,但它不起作用,它一直在循环播放
猜你喜欢
  • 2015-12-11
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多