我試圖得到的消息在某些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