如何用php来支配html url?

问题描述 投票:0回答:1

这是我要解析的URL。http:/www.tsetmc.comLoader.aspx?ParTree=151313&Flow=0

我使用simple_html_dom.php,但它无法读取HTML,因为HTML是编码的,所以我觉得应该解析在线和网页源码,有什么办法可以解析这个网站吗?

所以我想我应该解析在线和网页源码.有什么办法可以解析这个网站吗?

源代码是这样的。

<html>
  <body>
   <table class="table1">
    <tbody>
        <tr><th>***title</th>
            <th class='ltr'>***99/2/24 12:10</th>
        </tr>
        <tr><td colspan="2">***message text here<hr /></td></tr>
    </tbody>
  </table>
</body>

我的代码。

<?php
 require_once('simple_html_dom.php');
 $url = "http://www.tsetmc.com/Loader.aspx?ParTree=151313&Flow=0";
 $html = file_get_html($url);
 foreach($html->find('th') as $element)
   echo $element->src . '<br>';
?>
php dom
1个回答
0
投票

问题,正如你所指出的是编码,它的 gzip 编码。你可以在curl中设置标志 CURLOPT_ENCODING 来解决这个问题。它的作用,由php-curl文档提供。

"Accept-Encoding: "头的内容。这样就可以对响应进行解码。支持的编码有 "identity"、"deflate "和 "gzip"。如果设置了空字符串"",则会发送一个包含所有支持的编码类型的头。

使用下面的php-curl代码来获取响应html,就像这样。

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://www.tsetmc.com/Loader.aspx?ParTree=151313&Flow=0",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "gzip",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

然后你可以使用响应html $response 直接在 simple_html_dom.php 来解析dom树。

这是一个工作版本的代码。http:/phpfiddle.orgmaincodegb66-3kzq

© www.soinside.com 2019 - 2024. All rights reserved.