簡體   English   中英

<a>使用PHP</a>從<a>標簽中獲取</a> href值

[英]Scraping href value from <a> tag using PHP

我正在使用simple_html_dom從HTML標記檢索href屬性值。 我要抓取的數據放在HTML表中。 以下代碼成功指向數據並將其顯示為超鏈接。

// Include the library to use it.
include_once('simple_html_dom.php');

// Get the HTML from the file or website.
$html = file_get_html('source.html');

// Put all of the <a> tags into an array named $result
$result = $html -> find('table tbody tr td a');

// Run through the array using a foreach loop and print each link out using echo
foreach($result as $link) {
echo $link."<br/>";
}

但是我需要標簽中的href值,為此,我遵循了使用php解釋檢索<a href>標簽的多個值的方法,並使用了以下代碼,但它再次搜索了整個文檔並提取了位於表外部的所有鏈接。 我還用$result$link替換了$html ,但是那時它什么也不顯示。

preg_match_all("/href=\"(.*?)\"/i", $html, $matches);
print_r($matches);

如何使用上述方法之一從置於HTML表下的href屬性獲取值? 該表沒有任何類或ID可在選擇器中使用它。

注意:我還研究了以下內容: 使用php檢索鏈接值表單<a href>標記,但無法弄清楚。

我用preg_match代替preg_match_all ,然后用echo代替print。 以下代碼以第一個鏈接為目標,並將其顯示在屏幕上。

preg_match("/href=\"(.*?)\"/i", $html, $matches);
echo $matches;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM