使用php DOMXPath获取选项内容

I'm trying to get product sizes from all option elements or particular select tag that has an:

<select id="prodSize" name="prodSize">
    <option value="9274">10D</option>
    <option value="9275">10DD</option>
    <option value="9276">10E</option>
    <option value="9277">10F</option>
    <option value="9279">10G</option>
    <option value="9288">12D</option>
    <option value="9289">12DD</option>
    <option value="9290">12E</option>
    <option value="9291">12F</option>
    <option value="9301">14D</option>
    <option value="9302">14DD</option>
    <option value="9303">14E</option>
    <option value="9304">14F</option>
    <option value="9305">14FF</option>
    <option value="9315">16D</option>
    <option value="9317">16E</option>
    <option value="9318">16F</option>
    <option value="9319">16FF</option>
    <option value="9320">16G</option>
</select>

I have tried using $x("//select[@id='prodSize']/option/text()") in chrome dev tools and it returned me all the values with no problem, but when I'm tying to get it with DOMXPath:

$options = $xpath->query("//select[@id='prodSize']/option/text()");

or:

$options = $xpath->query("*/select[@id='prodSize']/option");

I get:

object(DOMNodeList)#40 (1) { ["length"]=> int(0) } object(DOMNodeList)#29 (1) { ["length"]=> int(0) }
object(DOMNodeList)#39 (1) { ["length"]=> int(0) } object(DOMNodeList)#41 (1) { ["length"]=> int(0) }

I have added full code for clarity:

scrapCatUrl('http://.../shop-management/categories/maternity-lingerie.aspx',  "//ul[@class='lvl2 visible']/li/a/@href");

 function scrapCatUrl($path, $query){

    $xpath = scrap($path);
    $links = $xpath->query($query);
    foreach($links as $link){
        echo 'Category'.' - '.$url.$link->nodeValue . '<br>';
        scrapProdUrl($url.$link->nodeValue);
    }
}
 function scrapProdUrl($path){

    $xpath = scrap($path);
    $links = $xpath->query("//a[@class='thumbObj']/@href");
    $i = 0;
    foreach($links as $link){
        echo 'Product'.' - '.$url.$link->nodeValue . '<br>';
        getProdData($url.$link->nodeValue);
        if($i > 2){
            die();
        }
        $i++;
    }
}
function getProdData($path){
    $xpath = scrap($path);
    $description = $xpath->query("//meta[@name='description']/@content");
    $keywords = $xpath->query("//meta[@name='keywords']/@content");
    $title = $xpath->query("//h4[@class='h4-productdetail']/text()");
    $price = $xpath->query("//div[@class='productDetail']/span[@class='price']/text()");
    $images = $xpath->query("//div[@class='imgs']/img/@src");
    $fullDescription = $xpath->query("//div[@class='flash']/following-sibling::div[@class='clearer']/preceding-sibling::text()[preceding-sibling::div[@class='flash']]");
    $options = $xpath->query("//select[@id='prodSize']/option/text()");

    echo 'Meta Description'.' - '.$description->item(0)->nodeValue. '<br>';
    echo 'Meta Keywords'.' - '.$keywords->item(0)->nodeValue. '<br>';
    echo 'Title'.' - '.$title->item(0)->nodeValue. '<br>';
    echo 'Price'.' - '.$price->item(0)->nodeValue. '<br>';
    if($images->length > 1){
        foreach($images as $image){
            echo '<img src="'.$url.$image->nodeValue.'" />'. '<br>';
        }
    }
    else{
        echo '<img src="'.$url.$image->nodeValue.'" />'. '<br>';
    }
    foreach($options as $option){
        echo $option->nodeValue;
    }



}
 function scrap($path){
    $ch = curl_init($path);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $page = curl_exec($ch);
    $dom = new DOMDocument();
    @$dom->loadHTML($page);
    $xpath = new DOMXpath($dom);
    return $xpath;
}

I have tried few ways that people suggested here but getting the same result. I have no problems getting any other element from a page, title, images, descriptions everything except this one.

duanbanfei2875
duanbanfei2875 查看原始页面源。如果它不包含您想要的选项,则可能通过javascript填充。Domxpath似乎没有那种能力。你可以使用phantomjs或php-phantomjs包装器。
大约 6 年之前 回复
dongxixian7803
dongxixian7803 我认为这个大小列表是由javascript生成的,是否可以在启动所有javascript函数后获取元素?
大约 6 年之前 回复
drdawt9210
drdawt9210 启用错误,没有发现问题,仍然获得除大小以外的所有内容:(
大约 6 年之前 回复
dtbc37573
dtbc37573 尝试启用错误报告(error_reporting(E_ALL);ini_set('display_errors','1');)..函数报废($path,$query){需要两个参数但你只提供一个:$xpath=报废($路径);
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐