【问题标题】:How to get text within td element from remote webpage via PHP如何通过 PHP 从远程网页获取 td 元素中的文本
【发布时间】:2018-12-31 04:43:51
【问题描述】:

我设置了一个温度监视器,我想将一些数据用于其他事情(cron 作业等)。可以从我们的本地网络 (192.168.123.123) 访问来自传感器的数据。有问题的元素是:

<td id="1E5410ECC9D90FC3-entity-0-measurement-0" class="">69.08</td>
<!-- I NEED THE 69.08 -->

我无法通过 ajax 执行此操作,因为我收到 Allow-Access-Origin 错误 (CORS)。

我试过这个:

$url = 'http://192.168.123.123';
$content = file_get_contents($url);
$first = explode( '<div id="1E5410ECC9D90FC3-entity-0-measurement-0">' , $content );
$second = explode("</div>" , $first[0] );

echo $second[0];

但我得到了这个:

��UMS�0��+��$���94С�2����؋-�%#Ʉ�뻲���Bۓ%����ݷr��m4�yyF*_+ry���ӈP������S��|��&�ȵ�2���}��V�7ǜO��dz�[�� (�!�_2��$�/�p/ g�=B� D����<��1�#�=h���J�˨�'��I^ ��g7��=�=��^�0��ϔ����p�Q��L��I�%TF�Q�)    ������;c��o$��a����g��mWr�ܹ��;�(��bE��O�i� ��y�҉)f=�6=�,2� �#I��s����>����kNƕt/W2^��@ Xp�3^݅$ѵ��T U�ʲ�@f��db�ԁ%��b�`G|��D�{????sι1�� ]#2ZH�(1;&�h8��^0er��3���D�Q�5B�u� ^!5X:�{a U\:߰0�~Ɍ�3+S�^1��qB:�g����C>�.�P~n��$\֢D����%J+�b�ELc�Gq���K �]��xV��j�[���Ԧ��nAɍ��<�ZT@���zc�Q(f܁�(~�^�ZKwk:8�·n>��(=�"aB)�Fl5�b]/�_�$���_��ɴ��9�H}��B    [#�V�ԅp��r�g�A�j���2����Ju*������{�bY�,O4�����M��B�#�e���,�   ��_֔���o����

如何正确获取特定 div id 中的“td”文本?

【问题讨论】:

  • 检查你的页面编码和PHP编码。
  • 先检查内容,再爆echo $content = file_get_contents($url);
  • 否则有一个库 phpQuery 可以使用 jquery 之类的选择器并解析 HTML。
  • @Rohit.007 如果我这样做,它仍然会在我的问题底部显示乱码。

标签: php html ajax cross-domain


【解决方案1】:

您正在尝试从&lt;td id="1E5410ECC9D90FC3-entity-0-measurement-0" class=""&gt; 而不是&lt;div id="1E5410ECC9D90FC3-entity-0-measurement-0"&gt; 检索数据,所以不是从&lt;div&gt;,所以只需将其更改为:

$url = 'http://192.168.123.123';
$content = file_get_contents($url);
$first = explode( '<td id="1E5410ECC9D90FC3-entity-0-measurement-0">' , $content );
$second = explode("</td>" , $first[0] );

echo $second[0];

还是我疯了?

【讨论】:

  • 很抱歉。你是对的。不过,在我的问题中,我仍然得到上面显示的胡言乱语。
  • 别担心,也许这可能是一个解决方案:stackoverflow.com/questions/5045598/…
  • 也试过了。收到 PHP 警告:DOMDocument::loadHTML(): Invalid char in CDATA 0x8 in Entity, line: 1
  • 这可能是因为您尝试从中获取内容的文件缺少/无效的文档类型声明
  • 我发现这个临时监控系统有一个 API,我可以通过 JSON 访问所有内容。现在会研究如何做到这一点,但我会留下这个,以防其他人决定回答并且有人需要知道如何按照最初的方式去做。
【解决方案2】:

第 1 步:

我建议使用 php 的 curl 库来管理和配置您的 Web 请求/响应。 使用这种机制可以让您更好地管理/控制编码、压缩和加密。

http://php.net/manual/en/book.curl.php

// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "http://192.168.123.123");        

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

第 2 步:

让我们从 Web 服务器返回的响应字符串中提取详细信息。我建议使用 PHP 的 PCRE 函数 preg_match 来提取所需的数据。

http://php.net/manual/en/ref.pcre.php

// Looking for <td id="1E5410ECC9D90FC3-entity-0-measurement-0" class="">69.08</td>
$pattern = '/id="1E5410ECC9D90FC3-entity-0-measurement-0".*>([\d]{1,2}?\.[\d]{1,2})<\//';

// run the regex match and collect the hit
preg_match($pattern, $output, $matches);

// print_r of the array
/*
Array
(
    [0] => id="1E5410ECC9D90FC3-entity-0-measurement-0" class="">69.08</
    [1] => 69.08
)
*/

// Print out the result to check
echo $matches[1];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    相关资源
    最近更新 更多