加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在html中找到一个元素并将其爆炸为库存

发布时间:2020-12-13 22:51:25 所属栏目:PHP教程 来源:网络整理
导读:我想在页面中检索 HTML元素. h2 id="resultCount" class="resultCount" span Showing 1 - 12 of 40,923 Results /span/h2 我必须在我的php中获得测试结果的总数. 现在,我得到了h2标签之间的所有内容,我第一次用空间爆炸. 然后我用逗号再次爆炸以连接能够转换
我想在页面中检索 HTML元素.

<h2 id="resultCount" class="resultCount">

    <span>

        Showing 1 - 12 of 40,923 Results

    </span>

</h2>

我必须在我的php中获得测试结果的总数.

现在,我得到了h2标签之间的所有内容,我第一次用空间爆炸.
然后我用逗号再次爆炸以连接能够转换欧洲格式的数字结果.一切都完成后,我测试我的数字结果.

define("MAX_RESULT_ALL_PAGES",1200);    
$queryUrl = AMAZON_TOTAL_BOOKS_COUNT.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&page=".$pageNum;
                $htmlResultCountPage = file_get_html($queryUrl);
                $htmlResultCount = $htmlResultCountPage->find("h2[id=resultCount]");
                $resultCountArray = explode(" ",$htmlResultCount[0]);

                $explodeCount = explode(',',$resultCountArray[5]);
                  $europeFormatCount = '';
                  foreach ($explodeCount as $val) {
                           $europeFormatCount .= $val;
                   }
                if ($europeFormatCount > MAX_RESULT_ALL_PAGES) {*/

                    $queryUrl = AMAZON_SEARCH_URL.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&page=".$pageNum;

                }

目前,结果总数没有很好地恢复,即使应该也没有发生.

有人可以解决这个问题或任何其他方式吗?

解决方法

我只是将页面作为字符串(而不是html)获取并使用正则表达式来获取结果总数.代码看起来像这样:

define('MAX_RESULT_ALL_PAGES',1200);

$queryUrl    = AMAZON_TOTAL_BOOKS_COUNT . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;
$queryResult = file_get_contents($queryUrl);

if (preg_match('/ofs+([0-9,]+)s+Results/',$queryResult,$matches)) {
    $totalResults = (int) str_replace(','',$matches[1]);
} else {
    throw new RuntimeException('Total number of results not found');
}

if ($totalResults > MAX_RESULT_ALL_PAGES) {
    $queryUrl = AMAZON_SEARCH_URL . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;
    // ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读