【问题标题】:Web Crawling a website returns empty (using preg_match_all)网络爬取网站返回空(使用 preg_match_all)
【发布时间】: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


【解决方案1】:

class="FootNotes2" 类不在 tr 中,但在 td 中,也许这会改变你的结果

编辑:

您的 Rexeg 在这种情况下不正确。您搜索&lt;td class="FootNotes2",但在&lt;tdclass="FootNotes2" 之间,您还有其他属性

将您的正则表达式更改为 &lt;td .*? class="FootNotes2"&gt;(.*?)&lt;\/td&gt; 可能会对您有所帮助

您可以使用https://regex101.com/ 进行测试。我在其中删除了 curl 响应并更改了您的正则表达式以在那里进行测试

再次编辑:

我仔细查看了您的代码并查看了您尝试抓取的网站。 但是你的代码中有很多错误。在您的循环中,您假设值存在而不进行检查,然后您尝试重新计算您尝试抓取的页面中不存在的值和模式。复制该页面的 html 并研究它并在正则表达式测试器中对其进行测试,还将您的结果 var_dump 到您从函数中返回的内容中,并编写检查以确保您想要使用的数据存在。一步一步做,你会得到你的结果。

【讨论】:

  • 不幸的是不,它没有。但是谢谢你提出来。
  • 您在milesfeed_links=[]; 中也有错字,您错过了 $ 符号
  • @HarishK 我更新了我的答案,我还注意到你的 rexeg 中有一个错字
  • 按照你说的做了。还没有运气。 preg_match_all('&lt;td .*? class="FootNotes2"&gt;(.*?)&lt;\/td&gt;',$milesfeed,$links); 感谢您的宝贵时间。
  • 那是因为您还需要更改您的第二个 rexeg 函数。我使用了/&lt;td .* class="FootNotes2"&gt;(.*?)&lt;\/td&gt;/ 并在我的$links[1] 中获得了 90 个结果
【解决方案2】:
<?php
    function con(){
            $q=mysqli_connect('localhost','root','','usmle');
            return $q;
    }
    function addquery($a,$b,$c,$d){
            $a=mysqli_real_escape_string(con(),$a);
            $b=mysqli_real_escape_string(con(),$b);
            $c=mysqli_real_escape_string(con(),$c);
            $d=mysqli_real_escape_string(con(),$d);
            mysqli_query(con(),"insert into query(Query,QueryBy,QueryLink,Date)values('$a','$b','$c','$d')");
    }
    function addreply($a,$b){
        $a=mysqli_real_escape_string(con(),$a);
        $b=mysqli_real_escape_string(con(),$b);
        mysqli_query(con(),"insert into replies(QueryID,Reply)values($a,'$b')");
    }
    
    function lastID(){
        $q=mysqli_query(con(),"select MAX(QueryID) as LastID from query LIMIT 1");
        return $q;
    }
    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;
    }
    function cleannr($str)
{
//     $str = str_replace("£", "", $str);
    $str = str_replace(array("\r\n", "\n\r", "\n", "\r"), ',', $str);
    $str = str_replace("\n" , "" , $str);
    $str = trim(strip_tags($str));
    return str_replace("," , " " , $str);
}
$set=0;
    $athomepage  = getSslPage('http://www.usmleforum.com/forum/index.php?forum=1&Page=1'); 
    preg_match_all('/<td width="64%" height="25" class="FootNotes2"><a href="(.*?)" target="_top" class="Links2">(.*?)<\/a>(.*?)<\/td>/s',$athomepage,$pages);
    
                /*  $count = $pages[2][0];
                 $total_pages = 1;
                
                 if($count > 0){
                     $pages = $pages[0][$count-1];
                     $total_pages = strip_tags($pages);
                
                 } */
                 // echo $total_pages;
                 // die;
            // echo "<pre>".print_r($listres,true)."</pre>";
                // die;
                foreach($pages[1] as $links){
                    ob_flush();
                    flush();
                    $set++;
                    echo 'Query No.'.$set.'<br />'; 
                    //echo "https://www.immobilienscout24.de/Suche/controller/exposeNavigation/goToExpose.go?exposeId=".$links."<br>";
                     //die;
                     $link="http://www.usmleforum.com".$links;
                     $ipage=getSslPage("http://www.usmleforum.com".$links."");
                     preg_match_all('/<td width="95%" valign="top" colspan="2" class="FormText2">(.*?)<\/td>/s',$ipage,$query);
                     preg_match_all('/<td width="97%" colspan="2" valign="top" class="FootNotes2">(.*?)<\/td>/s',$ipage,$by);
                    
                     $explodation=explode("-",$by[1][0]);
                    $date=$explodation[1];
                    $name=$explodation[0];
                    $actualquery=cleannr($query[1][0]);
                    echo '<h2>Query : </h2><br />';
                    echo 'Query : '.$actualquery.'<br />'; 
                    echo 'Query By : '.$name.'<br />'; 
                    echo 'Link : '.$link.'<br />'; 
                    echo 'Date : '.$date.'<br />'; 
                    
                    addquery($actualquery,$name,$link,$date);
                    
                    $id=lastID();
                    foreach($id as $ids){
                        $lastID=$ids["LastID"];
                    } 
                echo '<h2>Replies : </h2><br />';
                for($i=1;$i<count($query[1]);$i++){
                    if($query[1][$i]!=""){
                        $replyquery=cleannr($query[1][$i]);
                        
                        echo 'Reply : '.$replyquery.'<br />';                   
                        addreply($lastID,$replyquery);
                    }
                }
                
                     
                }
                    echo "<center><h2>Scraping Done</h2></center>";
?>

成功了。

【讨论】:

    猜你喜欢
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2013-11-23
    • 2019-10-26
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多