dongzhou4727 2014-11-23 08:09
浏览 38
已采纳

PHP - 无限循环的问题

I have no clue what I am doing wrong. EVERY time it goes through it just keeps looping and pulling all of the cities listed in the same row and placing them with the state, then when it goes to the next state it starts at the correct place, but still keeps going. I've tried for 4 hours I just can't figure it out.

$url = 'http://www.craigslist.org/about/sites';
$output = file_get_contents($url); 
$doc = new DOMDocument();

libxml_use_internal_errors(true); //Supress Warnings for HTML5 conversion issue
$doc->loadHTML($output);
libxml_use_internal_errors(false); //Start Showing Errors

$xpath = new DOMXpath($doc);    

foreach ($xpath->query('//h1') as $e) {
    $country = $e->nodeValue;
    $list = array();
    foreach ($xpath->query('./following-sibling::div[@class="colmask"]', $e) as $li) {

        foreach ($xpath->query('//div/h4', $e) as $div) {
            $state = $div->nodeValue;


            foreach ($xpath->query('./following-sibling::ul/li', $div) as $div2) {                      

                $href =  $div2->getAttribute("href");
                $text = trim(preg_replace("/[
]+/", " ", $div2->nodeValue));
                echo 'Country: ' . $country . ' State: ' . $state . ' CITY: text['. $text . '] href[' . $href . '] <br/><br/><br/>';


            }


        }

    }
}  
  • 写回答

2条回答 默认 最新

  • duanhuan6336 2014-11-24 00:50
    关注

    Had someone named DuffyDake answer my question. Here is the answer..

    foreach ($xpath->query('./following-sibling::ul[1]/li', $div) as $div2) {                      
    
                    $href =  $div2->getAttribute("href");
                    $text = trim(preg_replace("/[
    ]+/", " ", $div2->nodeValue));
                    echo 'Country: ' . $country . ' State: ' . $state . ' CITY: text['. $text . '] href[' . $href . '] <br/><br/><br/>';
    
    
                }
    

    The missing piece is the [1] to reference to the first UL found and not anything past that

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题