【问题标题】:scraping a specific html element with curl php使用 curl php 抓取特定的 html 元素
【发布时间】:2016-07-19 19:31:19
【问题描述】:
<?php

$url = 'http://url.com/usersvalue.html/';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);


print ($result);


?>

我正在尝试制作一个仅从网站输出特定行的脚本,该行是 html 及其

"<td>users</td><td>100</td>"

。我正在尝试输出 "&lt;td&gt;100&lt;/td&gt;" 的值,此时为 100,每次 $url 网站上的值发生变化时..

$url 网站的 html:

<html><head></head><body style='font-family:Verdana'>
<h2>Status</h2><hr>
<table cellpadding='6' cellspacing='0' border='0'>
<tr bgcolor='#eeeeee'><th align='left'>Key</th><th align='left'>Value</th></tr>
<tr>
<td>users</td><td>100</td>
</tr>
<tr>
<td>uptime</td><td>00</td>
</tr>
<tr>
<td>zones</td><td>0</td>
</tr>
<tr>
<td>rooms</td><td>0</td>
</tr>
<tr>
<td>version</td><td>0</td>
</tr>
</table><hr>
</body></html>

【问题讨论】:

  • 请提供更多线索,可能是您的受害者或 HTML 内容的结构。我会帮你的
  • 添加了html内容。
  • 试试我的源码兄弟 xD

标签: php html curl web-scraping


【解决方案1】:

你可以使用我的资源

function getStr($string,$start,$end){
    $str = explode($start,$string,2);
    $str = explode($end,$str[1],2);
    return $str[0];
}
echo getStr($result,'<td>users</td><td>','</td>');

if (!preg_match('#<td>users</td><td>([0-9]+)</td>#', $result, $result_preg)) { 
    die('bad dsid'); 
}else{
    echo $result_preg[1];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    相关资源
    最近更新 更多