2014-01-15 43 views
0

我试图使用名称= Reeks来获取选择列表的值,您可以在此页面上找到:http://www.volleyvvb.be/?page_id=1083。选择列表如下:从选项+ XML中选择值而不是文本DOM

<select class="vvb_tekst_middelkl" name="Reeks" onchange="this.form.submit();"> 
    <option value="%" selected="">ALLE REEKSEN</option> 
    <option value="Liga A H">ETHIAS VOLLEY LEAGUE</option> 
    <option value="Liga B H">LIGA B HEREN</option> 
    <option value="Ere D">LIGA A DAMES</option> 
    ... 
</select> 

这是我得到的选择列表:

$html = file_get_contents("http://www.volleyvvb.be/?page_id=1083"); 

$crawler = new Crawler($html); 

$crawler = $crawler->filter("select[name='Reeks']"); 
foreach ($crawler as $domElement) { 
    foreach($domElement->childNodes as $child) { 
     $value = $child->nodeValue; 
     var_dump($value); 
    } 
} 

我目前看到的所有​​3210像ALLE REEKSEN, ETHIAS VOLLEY LEAGUE之间的线条。但我也喜欢像Liga A H这样的价值观......我如何选择它们?

回答

1

下面的代码

<?php 
require_once("resources/simple_html_dom.php"); 
$html = file_get_contents("http://www.volleyvvb.be/?page_id=1083"); 
$doc = str_get_html($html); 

$select = $doc->find("select[name='Reeks']"); 
foreach ($select as $domElement) { 
    $child = $domElement->find('option'); 
    foreach($child as $option) { 
     echo $option->getAttribute('value')."<br>"; 
    } 
} 
?> 

给我你的要求的输出。

Liga A H 
Liga B H 
Ere D 
... 

为DomCrawler组件的等效是

$value = $child->nodeValue; // LIGA B HEREN, ... 
$attribute = $child->attr('value'); // Liga B H, ... 

详情看一看的Documentation here

+0

谢谢!我用你的第二个解决方案:$ child-> getAttribute('value'); – nielsv

0

你需要使用each method for crawlerattr mehtod for crawler

试试这个:

$crawler->filter("select[name='Reeks']")->each(function ($node, $i) { 
    echo $node->text();  -> // to print the value of html 
    echo $node->attr('value'); -> // to print the value of value attrbuite 
}); 
相关问题