如何解析a<;的属性值;a>;PHP中的标记
我正试图解析美国大学和学院数据库的html页面。我写的代码确实获取了大学的名称,但我无法获取它们各自的url地址如何解析a<;的属性值;a>;PHP中的标记,php,parsing,dom,html-parsing,Php,Parsing,Dom,Html Parsing,我正试图解析美国大学和学院数据库的html页面。我写的代码确实获取了大学的名称,但我无法获取它们各自的url地址 public function fetch_universities() { $url = "http://www.utexas.edu/world/univ/alpha/"; $dom = new DOMDocument(); $html = $dom->loadHTMLFile($url); $dom->preserveW
public function fetch_universities()
{
$url = "http://www.utexas.edu/world/univ/alpha/";
$dom = new DOMDocument();
$html = $dom->loadHTMLFile($url);
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('table');
$tr = $tables->item(1)->getElementsByTagName('tr');
$td = $tr->item(7)->getElementsByTagName('td');
$rows = $td->item(0)->getElementsByTagName('li');
$count = 0;
foreach ($rows as $row)
{
$count++;
$cols = $row->getElementsByTagName('a');
echo "$count:".$cols->item(0)->nodeValue. "\n";
}
}
这是我目前拥有的代码
请告诉我如何获取属性值
谢谢如果您有对元素的引用,您只需使用,因此可能:
echo "$count:".$cols->item(0)->getAttribute('href') . "\n";