我使用Simple HTML DOM Parser进行解析,但速度太慢了。所以我选择了cURL。我通过一些博客学习。现在我打印显示两个标签之间的href。
<?php
class tagSpider
{
var $crl;
var $html;
var $binary;
var $url;
function tagSpider()
{
$this->html = "";
$this->binary = 0;
$this->url = "";
}
function fetchPage($url)
{
$this->url = $url;
if (isset($this->url)) {
$this->ch = curl_init ();
curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($this->ch, CURLOPT_URL, $this->url);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, $this->binary);
$this->html = curl_exec($this->ch);
curl_close ($this->ch);
}
}
function parse_array($beg_tag, $close_tag)
{
preg_match_all("($beg_tag.*$close_tag)siU", $this->html, $matching_data);
return $matching_data[0];
}
}
?>
<?php
$urlrun="http://m4.cricbuzz.com/";
$stag='<span>';
$etag="</span>";
$tspider = new tagSpider();
$tspider->fetchPage($urlrun);
$linkarray = $tspider->parse_array($stag, $etag);
foreach ($linkarray as $result) {
echo strip_tags($result, '<br><div>');
echo "<br>-<br>";
}
?>
如何使用相同的程序显示href
答案 0 :(得分:2)
我看到你只是简单地复制并粘贴其他人的代码,却没有真正理解它实际在做什么(这很好!我是新手时做的)
你应该注意到代码是在2个单独的部分中切割的。第二部分应该在html body tabe中,因为它是打印html代码。只需在其周围添加html和body标记
<html>
<body>
<?php
$urlrun="http://www.yahoo.com/";
$stag='<span>';
$etag="</span>";
$tspider = new tagSpider();
$tspider->fetchPage($urlrun);
$linkarray = $tspider->parse_array($stag, $etag);
foreach ($linkarray as $result) {
echo strip_tags($result, '<br><div>');
echo "<br>-<br>";
}
?>
</body>
</html>
编辑:如果你想要链接,它更像是一个正则表达式的东西。
<html>
<body>
<?php
$urlrun="http://www.google.com/";
$stag='href\=\"';
$etag="\"";
$tspider = new tagSpider();
$tspider->fetchPage($urlrun);
$linkarray = $tspider->parse_array($stag, $etag);
foreach ($linkarray as $result) {
echo strip_tags($result, '<br><div>');
echo "<br>-<br>";
}
?>
</body>
</html>
这将为您提供......格式的东西。
HREF = “http://www.google.com/imghp?tab=wi”
HREF = “http://maps.google.com/maps?tab=wl” 我相信你可以弄清楚其余的就像摆脱字符串
的href =部分一样