dqyat62284 2015-02-15 18:51
浏览 31
已采纳

PHP简单DOM解析器返回布尔值/在刮擦方法中为空

Currently trying to scrape some HTML using a DOM scraping library called PHP Simple HTML Dom Parser.

I have the following method:

public function getFourLevels() {
    // Iterate through the four pollen levels [Wunderground only has four day
    // pollen prediction]
    for($i = 0; $i < 4; $i++) {

        // Get the raw level
        $rawLevels = $this->html
            ->find("td.text-center.even-four", $i)
            ->plaintext;

        // Clean the raw level
        $level = substr(
            $rawLevels,
            PollenBuddy::LEVELS
        );

        // Push each date to the dates array
        array_push($this->levels, $level);
    }

    return $this->levels;
}

The above method is my attempt at scraping the following HTML:

    <td class="text-center even-four">
    <strong>Sunday</strong>
    <div>February 15, 2015</div>
    </td>
    <td class="text-center even-four">
    <strong>Monday</strong>
    <div>February 16, 2015</div>
    </td>
    <td class="text-center even-four">
    <strong>Tuesday</strong>
    <div>February 17, 2015</div>
    </td>
    <td class="text-center even-four">
    <strong>Wednesday</strong>
    <div>February 18, 2015</div>
    </td>

Here is the source document.

The result I get from the above function using var_dump is:

array(4) {
  [0]=>
  bool(false)
  [1]=>
  bool(false)
  [2]=>
  bool(false)
  [3]=>
  bool(false)
}

Not quite sure what the issue. If someone could offer me some advice - thank you!

  • 写回答

1条回答 默认 最新

  • dongzhan1948 2015-02-15 19:00
    关注

    Using:

            // Get the raw level
            $rawLevel = $this->html
                ->find("td.even-four", $i)
                ->plaintext;
    

    Returns the right data.

    Found from this link: simple html dom - space in class name

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line