Development case of implementing simple crawler in PHP

小云云
Release: 2023-03-21 20:14:01
Original
1602 people have browsed it

Sometimes because of work and our own needs, we will browse different websites to obtain the data we need, so crawlers come into being. The following is my process of developing a simple crawler and the problems I encountered.

To develop a crawler, you must first know what your crawler is going to be used for. I want to use it to find articles with specific keywords on different websites and get their links so that I can read them quickly.

According to personal habits, I first need to write an interface and clarify my ideas.

1. Go to different websites. Then we need a url input box.
2. Find articles with specific keywords. Then we need an article title input box.
3. Get the article link. Then we need a display container for search results.


<p class="jumbotron" id="mainJumbotron">
 <p class="panel panel-default">
 
  <p class="panel-heading">文章URL抓取</p>
 
  <p class="panel-body">
   <p class="form-group">
    <label for="article_title">文章标题</label>
    <input type="text" class="form-control" id="article_title" placeholder="文章标题">
   </p>
   <p class="form-group">
    <label for="website_url">网站URL</label>
    <input type="text" class="form-control" id="website_url" placeholder="网站URL">
   </p>
 
   <button type="submit" class="btn btn-default">抓取</button>
  </p>
 </p>
 <p class="panel panel-default">
 
  <p class="panel-heading">文章URL</p>
 
  <p class="panel-body">
   <h3></h3>
  </p>
 </p>
</p>
Copy after login

Add the code directly, and then add some style adjustments of your own, and the interface is complete:

Then The next step is to implement the function. I use PHP to write it. The first step is to obtain the html code of the website. There are many ways to obtain the html code. I will not introduce them one by one. Here I use curl to obtain and pass in You can get the html code from the website url:


private function get_html($url){
 
 $ch = curl_init();
 
 $timeout = 10;
 
 curl_setopt($ch, CURLOPT_URL, $url);
 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
 curl_setopt($ch, CURLOPT_ENCODING, &#39;gzip&#39;);
 
 curl_setopt($ch, CURLOPT_USERAGENT, &#39;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36&#39;);
 
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 
 $html = curl_exec($ch);
 
 return $html;
 
}
Copy after login

Although you get the html code, you will soon encounter a problem, that is, the encoding problem, which may make you The next step of matching is in vain. Here we uniformly convert the obtained html content to utf8 encoding:


$coding = mb_detect_encoding($html);
if ($coding != "UTF-8" || !mb_check_encoding($html, "UTF-8"))
$html = mb_convert_encoding($html, &#39;utf-8&#39;, &#39;GBK,UTF-8,ASCII&#39;);
Copy after login

得到网站的html,要获取文章的url,那么下一步就是要匹配该网页下的所有a标签,需要用到正则表达式,经过多次测试,最终得到一个比较靠谱的正则表达式,不管a标签下结构多复杂,只要是a标签的都不放过:(最关键的一步)


$pattern = &#39;|<a[^>]*>(.*)</a>|isU&#39;;
preg_match_all($pattern, $html, $matches);
Copy after login

匹配的结果在$matches中,它大概是这样的一个多维素组:


array(2) { 
 [0]=> 
 array(*) { 
  [0]=>
  string(*) "完整的a标签"
  .
  .
  .
 }
 [1]=>
 array(*) {
  [0]=>
  string(*) "与上面下标相对应的a标签中的内容"
 }
}
Copy after login

只要能得到这个数据,其他就完全可以操作啦,你可以遍历这个素组,找到你想要a标签,然后获取a标签相应的属性,想怎么操作就怎么操作啦,下面推荐一个类,让你更方便操作a标签:


$dom = new DOMDocument();
 
@$dom->loadHTML($a);//$a是上面得到的一些a标签
 
$url = new DOMXPath($dom);
 
$hrefs = $url->evaluate(&#39;//a&#39;);
 
for ($i = 0; $i < $hrefs->length; $i++) {
 
 $href = $hrefs->item($i);
 
 $url = $href->getAttribute(&#39;href&#39;); //这里获取a标签的href属性
 
}
Copy after login

当然,这只是一种方式,你也可以通过正则表达式匹配你想要的信息,把数据玩出新花样。

得到并匹配得出你想要的结果,下一步当然就是传回前端将他们显示出来啦,把接口写好,然后前端用js获取数据,用jquery动态添加内容显示出来:


var website_url = &#39;你的接口地址&#39;;
$.getJSON(website_url,function(data){
 if(data){
  if(data.text == &#39;&#39;){
   $(&#39;#article_url&#39;).html(&#39;<p><p>暂无该文章链接</p></p>&#39;);
   return;
  }
  var string = &#39;&#39;;
  var list = data.text;
  for (var j in list) {
    var content = list[j].url_content;
    for (var i in content) {
     if (content[i].title != &#39;&#39;) {
      string += &#39;<p class="item">&#39; +
       &#39;<em>[<a href="http://&#39; + list[j].website.web_url + &#39;" target="_blank">&#39; + list[j].website.web_name + &#39;</a>]</em>&#39; +
       &#39;<a href=" &#39; + content[i].url + &#39;" target="_blank" class="web_url">&#39; + content[i].title + &#39;</a>&#39; +
       &#39;</p>&#39;;
     }
    }
   }
  $(&#39;#article_url&#39;).html(string);
});
Copy after login

上最终效果图:

The above is the detailed content of Development case of implementing simple crawler in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!