抓取网站与cURL请求不读取HTML代码


Scraping a website with cURL request not reading the HTML code

爬行http://www.mfinante.ro/infocodfiscal.html?cod=299不工作。它被重定向到其他位置。但是为什么呢?

<?php
$url = 'http://www.mfinante.ro/infocodfiscal.html?cod=299';
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_ENCODING ,"");
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
$html = curl_exec($curl);
$redirectURL = curl_getinfo($curl,CURLINFO_EFFECTIVE_URL );
curl_close($curl);
echo $html;
?>

我不明白为什么会发生这种事。

您可以使用htmlspecialchars()获取响应的源代码

echo htmlspecialchars($html);

很可能有一个javascript或元重定向在那里的某个地方。我的JS很差,我不能帮你。

如果你能找到,你可以构建一个正则表达式来找到URL,然后获取它的内容。