【发布时间】:2020-10-17 02:23:32
【问题描述】:
我正在尝试使用网络爬虫(下面的代码)提取此page 中列出的所有查询。但我好像漏掉了什么。
我的代码如下:
<?php
function getSslPage($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Chrome/36.0.1985.125");
$login = curl_exec($ch);
return $login;
}
$milesfeed = getSslPage('http://www.usmleforum.com/forum/index.php?forum=1');
preg_match_all('/<td class="FootNotes2">(.*?)<\/td>/s',$milesfeed,$links);
$milesfeed_links=[];
$milesfeed_text=[];
$fourth="abc";
$third="abc";
//$third="https://onemileatatime";
foreach($links[1] as $miles){
$milesfeed_text[] = strip_tags($miles);
preg_match_all('/<a target="_top" class="Links2" href="(.*?)">/s', $miles, $link);
$milesfeed_links[] = strip_tags($link[1][0]);
$first=explode("://",$link[1][0]);
$second=explode(".",$first[1]);
//print_r($second);
if($second[0]!=$third || $third=="abc"){
if($second[0]=="www"){
echo "<h3>".ucfirst($second[1])."</h3>";
}else{
echo "<h3>".ucfirst($second[0])."</h3>";
}
}
echo '<a href="'.$link[1][0].'" target="_blank">'.wordwrap(strip_tags($miles),30).'</a><br><br>';
$third=$second[0];
}
?>
我花了 4 个小时试图自己弄清楚。非常感谢任何帮助...
【问题讨论】:
-
您的 curl_exec 是否返回 html?还是那一个是空的
-
顺便说一句 - 值得一看 stackoverflow.com/questions/33795717/… 以了解有关使用
CURLOPT_SSL_VERIFYPEER,false的一般想法
标签: php web-crawler