我试图得到的消息在某些div
所有链接URL从这个网站php:如何从给定页面的特定div获取所有超链接?
要获得所有的链接,之后我查看源代码,但没有什么。
但有任何数据显示
可以在任何能够理解PHP
,Array()
和JS
帮助我,好吗?
这是我的代码来获取内容:
$html = file_get_contents("https://qc.yahoo.com/");
if ($result === FALSE) {
die("?");
}
echo $html;
我试图得到的消息在某些div
所有链接URL从这个网站php:如何从给定页面的特定div获取所有超链接?
要获得所有的链接,之后我查看源代码,但没有什么。
但有任何数据显示
可以在任何能够理解PHP
,Array()
和JS
帮助我,好吗?
这是我的代码来获取内容:
$html = file_get_contents("https://qc.yahoo.com/");
if ($result === FALSE) {
die("?");
}
echo $html;
假设,你想从给定的页面提取所有Anchor
标签与他们的超链接。
现在有一定的问题,在该网址上做file_get_contents
:
gzip
因此,克服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 &。
要查找在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>";
}
$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。你不需要大量的代码,只需要这一部分。
我有一个很难理解。如果您向我们展示'$ html'输入示例,以及您完成处理时想要的内容,这将有所帮助。只是一个小样本,足以让我们明白你想要做什么。 – BeetleJuice
hy @BeetleJuice有你检查http://stackoverflow.com/a/38396700/6516181,我的意思是,对不起,我没有先进的编码和关键字的名称。请你的帮助^^ – ane