如何使用类来卷曲其他站点的值

时间:2016-01-29 12:29:26

标签: php curl

我想从表格形式的网站获取vales

<table class='mytable'>
    <tr><td class='data'>Model</td> <td class='value'>Model 1</td></tr>
    <tr><td class='data'>Price</td> <td class='value'>10000</td></tr>
    <tr><td class='data'>Class</td> <td class='value'>Class new</td></tr>
    ....
    ....
    ....
</table>

我希望得到每个数据的价值,即将模型作为模型1获取,价格为10000,......

用于卷曲的PHP代码

<?php
    $myurl="http://www.example.com/page/";
    $curl = curl_init("$myurl");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($curl, CURLOPT_FAILONERROR, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    $page = curl_exec($curl);

    if($myurl!='')
        {
        if(!empty($curl))
        { 
            $pokemon_doc = new DOMDocument;
            libxml_use_internal_errors(true);
            $pokemon_doc->loadHTML($page);
            libxml_clear_errors(); //remove errors for yucky html
            $pokemon_xpath = new DOMXPath($pokemon_doc);
            $priceflip =$pokemon_xpath->evaluate('string(//table[@class="mytable"])');

            echo $priceflip;

        }
    }
?>

这是将整个内容作为段落

返回

但我想获取有关数据的单独值

0 个答案:

没有答案