我想从此页面提取所有团队名称和链接:
https://www.transfermarkt.fr/ligue-1/startseite/wettbewerb/FR1
我正在使用DOMXpath来匹配元素,但是使用以下代码,它什么也不会返回。
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = 'https://www.transfermarkt.fr/ligue-1/startseite/wettbewerb/FR1';
$html = get_data($url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach($xpath->query('//*[contains(concat( " ", @class, " " ), concat( " ", "hide-for-pad", " " ))]//*[contains(concat( " ", @class, " " ), concat( " ", "tooltipstered", " " ))]') as $v) {
echo $v->getAttribute("href") . PHP_EOL;
}
你知道为什么吗?
谢谢您。
答案 0 :(得分:1)
有时,一旦页面加载,HTML就会被Javascript更改。我看过该页面,我认为您应该能够从以下获得详细信息(请检查是否显示了正确的URL)...
$teams = $xpath->query('//td[@class="zentriert no-border-rechts"]/a[contains(concat( " ", @class, " " ), concat( " ", "vereinprofil_tooltip", " " ))]');
foreach($teams as $v) {
echo $v->getAttribute("href") . " - ";
echo $v->firstChild->getAttribute("alt").PHP_EOL;
}
这可能会产生重复,因此可以创建一个团队列表和类似这样的URL ...
$teams = [];
foreach($teams as $v) {
$teams[$v->firstChild->getAttribute("alt")] = $v->getAttribute("href");
}
这将为您提供团队名称列表(作为键)和URL作为值。