【问题标题】:Scrape desired data from html code using PHP使用 PHP 从 html 代码中抓取所需的数据
【发布时间】:2015-11-04 05:04:58
【问题描述】:

下面是我要从中获取一些数据的 html 代码。

<div class="NS_projects__stats">
    <div class="digits_4" id="stats">
        <div class="row">
            <div class="col col-12 mb1 stat-item">
                <div class="num h1 bold" data-backers-count="107" id="backers_count">
                    <data class="Project1135352094" data-format="number" data-value="107" itemprop="Project[backers_count]">107</data>
                </div>
                <span class="bold h5">backers</span>
            </div>
            <div class="col col-12 mb1 stat-item">
                <div class="num h1 bold nowrap" data-goal="8000.0" data-percent-raised="0.909875" data-pledged="7279.0" id="pledged">
                    <data class="Project1135352094" data-currency="EUR" data-format="shorter_money" data-precision="0" data-value="7279.0" data-without_code="true" itemprop="Project[pledged]">€7,279</data>
                    <span class="money eur project_currency_code"></span>
                </div>
                <span class="bold h5">
                    pledged of <span class="money eur no-code">€8,000</span>
                    <span class="mobile-hide">goal</span>
                </span>
            </div>
            <span data-duration="30.041666666666668" data-end_time="2015-11-27T14:32:42-05:00" data-hours-remaining="566.7967307435142" id="project_duration_data"></span>
            <div class="col col-12 stat-item">
                <div class="num h1 bold">23</div>
                <span class="text bold h5">days to go</span>
            </div>
        </div>
    </div>
</div>

我必须从上面的 html 代码中获取以下数据:

  • 107 位支持者
  • 承诺 7,279 欧元,目标 8,000 欧元
  • 还有 23 天

我成功抓取了第一个,但无法获取第二个和第三个。 下面是我的 PHP 代码(使用 CURL)来获取第一个。

$html = get($url); //get function uses CURL and gets html data
$pattern = "/<div class=\"num h1 bold\"(.*?)<\/div>/s";
preg_match($pattern,$htm,$match);
$match[1] = "<div".$match[1]."</div>";
return strip_tags($match[1]); 

【问题讨论】:

标签: php html curl


【解决方案1】:
$pattern = "/<div class=\"num h1 bold\"(.*?)<\/div>/s";
$pattern2 = "/<div class=\"col col-12 mb1 stat-item\"(.*?)<\/div>/s";
$pattern3 = "/<div class=\"col col-12 stat-item\"(.*?)<\/div>/s";

【讨论】:

  • 但是有两个名为“col col-12 mb1 stat-item”的类不会造成问题吗?
【解决方案2】:

试试这个,

function rip_tags($string) { 

    // ----- remove HTML TAGs ----- 
    $string = preg_replace ('/<[^>]*>/', ' ', $string); 

    // ----- remove control characters ----- 
    $string = str_replace("\r", '', $string);    // --- replace with empty space
    $string = str_replace("\n", ' ', $string);   // --- replace with space
    $string = str_replace("\t", ' ', $string);   // --- replace with space

    // ----- remove multiple spaces ----- 
    $string = trim(preg_replace('/ {2,}/', ' ', $string));

    return $string; 

}

$html = get($url); //get function uses CURL and gets html data
echo rip_tags($html);

结果:1​​07 名支持者承诺 7,279 欧元,目标是 23 天后达到 8,000 欧元
可以根据要求进一步修改。供参考,请查看here

【讨论】:

  • 非常感谢,我从来没有这样想过,但仍然存在自动将€转换为$的问题。我没有得到 8,000 欧元的 7,279 欧元的承诺,而是 8,850 美元目标的 8,051 美元的承诺。我应该声明一些字符编码或类似的东西吗?
  • 页面上是否还写了其他代码。如果是这样,你能不能分享一下。
  • 我正在从下面的链接kickstarter.com/projects/35540661/…获取内容
  • 请检查,curl返回的html数据有欧元符号或$符号。
【解决方案3】:

我建议将 HTML-String 解析为 HTML...

你可以使用http://php.net/manual/en/domdocument.loadhtml.php

或其他一些第 3 方解析器。 (我之前用过http://simplehtmldom.sourceforge.net,还不错)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2013-07-03
    • 2011-03-23
    • 1970-01-01
    相关资源
    最近更新 更多