【问题标题】:I cannot call only links in the div with preg_match or preg_replace我不能只用 preg_match 或 preg_replace 调用 div 中的链接
【发布时间】:2016-02-02 07:51:35
【问题描述】:

这是我的代码:

$curl = curl_init('http://www.houseoffraser.co.uk/');
$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13";

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_TIMEOUT, 400);
ini_set('max_execution_time', 300);

$page = curl_exec($curl);

if(curl_errno($curl)) // check for execution errors
{
    echo 'Scraper error: ' . curl_error($curl);
    exit;
}

$html= curl_close($curl);

$dom = new DOMDocument();
@$dom->loadHTML($html);

$regex = '/<nav class="hof-buttons">(.*?)<\/nav>/s';

if (preg_match($regex, $page, $list)) {
    echo  preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $list[0])."<br />";
} else {
    print "Not found";  
}

我试图只从 div 标签中获取 url 名称。但这只会给我错误。我主要想要这样的东西:

<div class="a"><a href="abc.php">a linki</a></div> 

在代码中它必须是这样的:

if ( preg_match($regex, $page, $list) ){}; 

echo  <a href="$list[1]"> $list[0]</a>;

但是当我使用它时,它会给我错误或没有数组。我想要这样的代码,但是如何将我想要的内容添加到 preg_match 中,或者如何调用 div 中的链接?

【问题讨论】:

  • ops .. 我错了地方(并且在代码中必须与 if ( preg_match($regex, $page, $list) ){}; echo $list[0] 类似;) 编辑了这个地方;并且在代码中必须类似于 if ( preg_match($regex, $page, $list) ){};回声 $list[1];
  • 请更新问题,而不是在此处添加评论。
  • 我不知道如何更新朋友O_O你能帮我吗?

标签: php curl preg-replace preg-match-all


【解决方案1】:

好的,这就是整个解决方案(如果这是您要寻找的)。
顺便说一句,没有 curl,只需 file_get_contents()这样做:

我接管了你的三步法:

  • 第 1 步:在 之间提取。
  • 第 2 步:提取
    之间的所有 hrefs
  • 第 3 步:从不同来源收集文本并进行清理。

代码

<?php
$page = file_get_contents('http://www.houseoffraser.co.uk/');

if($page===false) // check for execution errors
{
    echo 'Scraper error: ' . curl_error($curl);
    exit;
}

if ( preg_match_all('%<nav class=[\'"]{1,1}hof-buttons-set left[\'"]{1,1}>(.*?)</nav>%si', $page, $regs1, PREG_PATTERN_ORDER) ) {
    for ($x1 = 0; $x1 < count($regs1[0]); $x1++) {
        if ( preg_match_all('%<div.*?<a href=[\'"]{1,1}([^\'"]*?)[\'"]{1,1}>(.*?)</a>.*?</div>%sim', $regs1[1][$x1], $regs2, PREG_PATTERN_ORDER) ) {
            for ($x2 = 0; $x2 < count($regs2[0]); $x2++) {
            $link = $regs2[1][$x2];
            if (preg_match('/<img.*? title=[\'"]{1,1}(.*?)[\'"]{1,1}/sim', $regs2[2][$x2], $regs3)) {
                // No text, but image with title
                $text = $regs3[1];
            } elseif (preg_match('%<span.*?class=[\'"]{1,1}hof-label[\'"]{1,1}.*?>(.*?)</span>%sim', $regs2[2][$x2], $regs3)) {
                // Text in <span class="hof-label">...</span>
                $text = $regs3[1];
            } else {
                // Plain text
                $text = $regs2[2][$x2];
            }
                echo '<a href="'.$link.'" title="'.$link.'" target="_blank">' . trim($text) . '</a><br />';
            }    
        } else {
            echo '<span style="color:red; font-weight:bold;">HREF not found<span><br />';
        }
    }
} else {
    echo '<span style="color:red; font-weight:bold;">NAV not found<span><br />';
    exit;
}
?>

结果

文字:女性
链接:http://www.houseoffraser.co.uk/Women%27s+Designer+Clothing/03,default,sc.html

文字:连衣裙
链接:http://www.houseoffraser.co.uk/women%27s+designer+dresses/301,default,sc.html

[....]

【讨论】:

  • 它给了错误兄弟;你能举一个我的代码的例子吗?
  • 它像以前一样工作,但我不想那样......我想要列出链接名称,比如“ab.php?go=page”等。
  • 更新:整个解决方案。
  • 非常感谢你的兄弟,它工作了..但不仅仅是“class='hof-buttons'”类全名; “class=hof-buttons-set left”那么我怎么能在“hof-buttons-set”这个之后使用空字符..我也必须放“left”类..
  • 进行了更新:hof-buttons-set left(确切地说是“hof-buttons left”)而不是 hof-buttons.*?(以“hof-buttons”开头加上任何文本直到结束引号)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多