2016-07-15 138 views
-1

我试图得到的消息在某些div所有链接URL从这个网站php:如何从给定页面的特定div获取所有超链接?

要获得所有的链接,之后我查看源代码,但没有什么。

但有任何数据显示

可以在任何能够理解PHPArray()JS帮助我,好吗?

这是我的代码来获取内容:

$html = file_get_contents("https://qc.yahoo.com/"); 
if ($result === FALSE) { 
    die("?"); 
} 
echo $html; 
+0

我有一个很难理解。如果您向我们展示'$ html'输入示例,以及您完成处理时想要的内容,这将有所帮助。只是一个小样本,足以让我们明白你想要做什么。 – BeetleJuice

+0

hy @BeetleJuice有你检查http://stackoverflow.com/a/38396700/6516181,我的意思是,对不起,我没有先进的编码和关键字的名称。请你的帮助^^ – ane

回答

1

假设,你想从给定的页面提取所有Anchor标签与他们的超链接。

现在有一定的问题,在该网址上做file_get_contents

  1. 字符编码压缩,即gzip
  2. SSL的URL的验证。

因此,克服gzip字符编码的第一个问题,我们将使用卷曲如@ gregn3在他的回答提出。但他错过了使用CURL的能力来自动解压缩内容。

对于第二个问题,您可以按照this指南或从CURL的curl_setopt方法中禁用SSL验证。

现在将提取指定页面所有链接的代码是:

<?php 

$url = "https://qc.yahoo.com/"; 

# download resource 
$c = curl_init ($url); 
curl_setopt($c, CURLOPT_HTTPHEADER, ["Accept-Encoding:gzip"]); 
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($c, CURLOPT_ENCODING , "gzip"); 
curl_setopt($c, CURLOPT_VERBOSE, 1); 
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0); 
$content = curl_exec ($c); 

curl_close ($c); 

$links = preg_match_all ("/href=\"([^\"]+)\"/i", $content, $matches); 

# output results 
echo "url = " . htmlspecialchars ($url) . "<br>"; 
echo "links found (" . count ($matches[1]) . "):" . "<br>"; 
$n = 0; 
foreach ($matches[1] as $link) 
{ 
    $n++; 
    echo "$n: " . htmlspecialchars ($link) . "<br>"; 
} 

但是,如果你想要做提前HTML解析,那么你就需要使用PHP Simple HTML Dom Parser。在PHP Simple HTML Dom中,您可以使用jQuery选择器来选择div并获取anchor tags。这里是documentation &​​。

+0

谢谢@Deepak,我对CURL不是很熟悉,但现在我也了解这一点。 :) – gregn3

+0

不,我喜欢这个。这让我更加明白。谢谢你的描述和知识先生:*吻拥抱..#真棒btw你有什么社会,我想加你先生 – ane

+0

:)和对不起,我不知道* socmed *是什么。 –

0

要查找在HTML的各个环节,你可以使用preg_match_all()。

$links = preg_match_all ("/href=\"([^\"]+)\"/i", $content, $matches); 

该URL https://qc.yahoo.com/使用gzip压缩,所以你必须检测和使用功能gzdecode解压()。 (它必须安装在您的PHP版本中)

gzip压缩由Content-Encoding: gzip HTTP标头指示。您必须检查该标题,因此您必须使用curl或类似的方法来检索标题。 (file_get_contents()函数不会给你的HTTP头......这仅下载gzip压缩的内容,您需要检测,它被压缩,但对于需要读头)。

下面是一个完整例如:

<?php 

$url = "https://qc.yahoo.com/"; 

# download resource 
$c = curl_init ($url); 
curl_setopt ($c, CURLOPT_HEADER, true); 
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); 
$content = curl_exec ($c); 
$hsize = curl_getinfo ($c, CURLINFO_HEADER_SIZE); 
curl_close ($c); 

# separate headers from content 
$headers = substr ($content, 0, $hsize); 
$content = substr ($content, $hsize); 

# check if content is compressed with gzip 
$gzip = 0; 
$headers = preg_split ('/\r?\n/', $headers); 
foreach ($headers as $h) 
{ 
    $pieces = preg_split ("/:/", $h, 2); 
    $pieces2 = (count ($pieces) > 1); 
    $enc = $pieces2 && (preg_match ("/content-encoding/i", $pieces[0])); 
    $gz = $pieces2 && (preg_match ("/gzip/i", $pieces[1])); 
    if ($enc && $gz) 
    { 
     $gzip = 1; 
     break; 
    } 
} 

# unzip content if gzipped 
if ($gzip) 
{ 
    $content = gzdecode ($content); 
} 


# find links 
$links = preg_match_all ("/href=\"([^\"]+)\"/i", $content, $matches); 

# output results 
echo "url = " . htmlspecialchars ($url) . "<br>"; 
echo "links found (" . count ($matches[1]) . "):" . "<br>"; 
$n = 0; 
foreach ($matches[1] as $link) 
{ 
    $n++; 
    echo "$n: " . htmlspecialchars ($link) . "<br>"; 
} 
+1

hy @ gregn3谢谢理解我的帖子我不知道关键字,在我使用你的代码后,我得到了eroor,在这里我检查我的php 5.6.23,gzdecode确定,zlib扩展加载,**但** PHP致命错误:调用未定义的函数gzip_inflate()生成..为什么?请你帮忙。 – ane

+1

btw抱歉,我想给予upvote **但**感谢您的反馈!记录下少于15名声望的演员的投票,但不要更改公开显示的帖子分数#myrputation是坏T.T – ane

+0

例如,如果我打开表格原始网站有10个链接。 **但**当我卷曲的网站,他们只显示5个链接..如何显示所有链接? – ane

3
$html = new DOMDocument(); 
@$html->loadHtmlFile('https://qc.yahoo.com/'); 
$xpath = new DOMXPath($html); 
$nodelist = $xpath->query("//div[@id='news_moreTopStories']//a/@href"); 
foreach ($nodelist as $n){ 
echo $n->nodeValue."\n"; 
} 

您可以从您指定的div中获取所有链接。请确保您将div ID设为id='news_moreTopStories']。您正在使用xpath来查询div。你不需要大量的代码,只需要这一部分。

http://php.net/manual/en/class.domxpath.php

+0

hy先生,谢谢你帮助我们,这将为我增加更多的解决方案^^ – ane

+0

@ane不客气。我很高兴能够提供帮助。 – unixmiah

+0

是的,这是一个更好的解决方案,但它似乎没有解码gzip-ed内容。 – gregn3

相关问题