dqyat62284 2015-02-15 18:51 采纳率: 0%
浏览 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

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

报告相同问题?