Home > Backend Development > PHP Tutorial > Detailed explanation of examples of crawling web content in php

Detailed explanation of examples of crawling web content in php

coldplay.xixi
Release: 2023-04-09 13:02:02
forward
4068 people have browsed it

Detailed explanation of examples of crawling web content in php

Detailed explanation of examples of crawling web content in php

Method one:

Use the file_get_contents method to implement

  $url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml";
    $html = file_get_contents($url);
    //如果出现中文乱码使用下面代码
    //$getcontent = iconv("gb2312", "utf-8",$html);
    echo "<textarea style=&#39;width:800px;height:600px;&#39;>".$html."</textarea>";
Copy after login

The code is very simple and can be understood at a glance without explanation.

Method 2:

Use curl to implement

$url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml";
    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($ch);
curl_close($ch);

echo "<textarea style=&#39;width:800px;height:600px;&#39;>".$html."</textarea>";
Copy after login
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
Copy after login

Add this code to indicate that if the request is redirected, you can access the final request page, otherwise the request result will display the following content:

Related learning recommendations: php programming (video)

The above is the detailed content of Detailed explanation of examples of crawling web content in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:jb51.net
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