使用 Jsoup 提取具有相似 类 的跨度
Use Jsoup to extract a span with similar classes
从这样的事情...
<Head>
<Body>
<div id="content-wrap" class="relative right-side-nav">
...
<tr>
<td class="indent"><span>Mean Temperature</span></td>
<td>
<span class="wx-data"><span class="wx-value">55*</span><span class="wx- unit"> °F</span></span>
</td>
<td>
</td>
<td> </td>
</tr>
<tr>
<td class="indent"><span>Max Temperature</span></td>
<td>
<span class="wx-data"><span class="wx-value">66</span><span class="wx-unit"> °F</span></span>
</td>
<td>
<span class="wx-data"><span class="wx-value">59</span><span class="wx-unit"> °F</span></span>
</td>
<td>
</td>
</tr>
我想获得“*”即 55 处的平均温度值
不是 66 或 59
我该怎么做?
我向您分享了 this post, yet for a better performance you'd use xPath approach 中稍作修改的代码。
<?php
$curl = curl_init('<url of interest>');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($curl);
if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
curl_close($curl);
$regex = '#<span class="wx-value">(.*?)<\/span>#gs';
if ( preg_match_all($regex, $page, $list) )
print_r($list);
else
print "Not found";
?>
稍后您可以通过添加 (int) 符号将捕获的值从 55*
细化为 55
:(int)$list[0]
.
从这样的事情...
<Head>
<Body>
<div id="content-wrap" class="relative right-side-nav">
...
<tr>
<td class="indent"><span>Mean Temperature</span></td>
<td>
<span class="wx-data"><span class="wx-value">55*</span><span class="wx- unit"> °F</span></span>
</td>
<td>
</td>
<td> </td>
</tr>
<tr>
<td class="indent"><span>Max Temperature</span></td>
<td>
<span class="wx-data"><span class="wx-value">66</span><span class="wx-unit"> °F</span></span>
</td>
<td>
<span class="wx-data"><span class="wx-value">59</span><span class="wx-unit"> °F</span></span>
</td>
<td>
</td>
</tr>
我想获得“*”即 55 处的平均温度值 不是 66 或 59 我该怎么做?
我向您分享了 this post, yet for a better performance you'd use xPath approach 中稍作修改的代码。
<?php
$curl = curl_init('<url of interest>');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($curl);
if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
curl_close($curl);
$regex = '#<span class="wx-value">(.*?)<\/span>#gs';
if ( preg_match_all($regex, $page, $list) )
print_r($list);
else
print "Not found";
?>
稍后您可以通过添加 (int) 符号将捕获的值从 55*
细化为 55
:(int)$list[0]
.