我已经编写了以下代码,但它只是返回空数据:
enter code here
$code="CS225";
$url="https://cs.illinois.edu/courses/profile/{$code}";
echo $url;
$html = file_get_contents($url);
$pokemon_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$pokemon_doc->loadHTML($html);
libxml_clear_errors();
$pokemon_xpath = new DOMXPath($pokemon_doc);
$pokemon_row = $pokemon_xpath->query("//div[@id='extCoursesDescription']");
if($pokemon_row->length > 0){
foreach($pokemon_row as $row){
echo $row->nodeValue . "<br/>";
}
}
}
我要刮擦的网站是:https://cs.illinois.edu/courses/profile/cs225
课程内容似乎在加载时通过页面上加载了源。但是,如果您浏览已加载的来源,则可以...
<script type='text/javascript' src='//ws.engr.illinois.edu/courses/item.asp?n=3&course=CS225'></script>
您可以跟踪到URL http://ws.engr.illinois.edu/courses/item.asp?n=3&course=CS225
,这为您提供了实际内容。因此,使用此新的URL,而不是原始URL,您应该能够从那里提取信息。
尽管此内容全部包裹在document.write()
'S。
更新:
删除document()
位 - 一种简单的方法就是处理内容...
$html = file_get_contents($url);
$html = str_replace(["document.write('","');"], "", $html);
$html = str_replace('"', '"', $html);