doujia1679 2013-09-09 04:26
浏览 59

如何在PHP中使用cURL打印href

I used Simple HTML DOM Parser for Parsing but it was too slow. So I chose cURL. I learning through Some blogs. Now I print to display the href between two tags.

<?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>";
}
?> 

How to display the href using the same Program

  • 写回答

1条回答 默认 最新

  • dongye1934 2013-09-09 04:41
    关注

    I see you are simply copying and pasting someone else code without actually understand what it is actually doing (which is fine! I did it when I was a newbie)

    you should notice the code are cut in 2 separate section. the second part should be in a html body tabe since it is printing html code. simply add html and body tag around it

    <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>
    

    edit: if you want the link instead, it's more of a regular expression thing.

    <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>
    

    this will get you things in the format of...

    href="http://www.google.com/imghp?tab=wi"

    href="http://maps.google.com/maps?tab=wl" I am sure you can figure out the rest like getting rid of the href= part of the string

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题