How to crawl web content and images in php
This article mainly introduces the method of crawling web content and images with PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it.
Example 1:
<?php include_once("curl.php");/*这个文件要自己配置好*/ header("content-type:text/html;charset=utf8"); $pattern_title ="/<title>(.+)<//title>/";//标题匹配 $pattern_code = "/<tr><td><p>(.+)<//p><script/";//网页内容匹配 $url="http://www.freewl.com/freezcm/2011/0413/3185.html";//根网址 $DATA_CONTENT = CurlGet($url); $num=preg_match_all($pattern_title, $DATA_CONTENT, $match_title); for($i=0;$i<$num;$i++){ echo strlen($match_title[1][$i])."--".$match_title[1][$i]."<br />"; } echo "<hr />"; /*$trans = array(" "=>",", "<br />"=>"。"); $TRANS_CONTENT = strtr($DATA_CONTENT, $trans); echo $TRANS_CONTENT; */ $DATA_CONTENT=preg_replace('//s(?=/s)/', ' ', $DATA_CONTENT);//(?=pattern) 正向预查 $DATA_CONTENT=preg_replace('/[/n/r/t]/', '/r/n', $DATA_CONTENT);//回车换行 $DATA_CONTENT=preg_replace('/ /', ' ', $DATA_CONTENT); $num=preg_match_all($pattern_code, $DATA_CONTENT, $match_code); for($i=0;$i<$num;$i++){ echo $match_code[1][$i]."<br />"; } ?>
Example 2:
<?php /* author: ssh_kobe date: 20110615 */ set_time_limit(0);//抓取不受时间限制 function get_pic($pic_url) { //获取图片二进制流 $data=CurlGet($pic_url); /*利用正则表达式得到图片链接 $pattern_src = '/<img.*?\"([^\"]*(jpg|bmp|jpeg|gif)).*?>/';*/ $pattern_src = '/<img.*?src\=\"(.*\.jpg).*?>/';//只匹配jpg格式的图片 $num = preg_match_all($pattern_src, $data, $match_src); $arr_src=$match_src[1];//获得图片数组 //get_name($arr_src); get_name_2($arr_src); echo 'End!!!<br>'; return 0; } function get_pic_2($pic_url, $base_site) { //获取图片二进制流 $data=CurlGet($pic_url); /*利用正则表达式得到图片链接*/ $pattern_src = '/<img.*?\"([^\"]*jpg).*?>/';//只匹配jpg格式的图片 $num = preg_match_all($pattern_src, $data, $match_src); $arr_src=$match_src[1];//获得图片数组 $arr_src=rev_site($arr_src, $base_site); get_name($arr_src); echo 'End!!!<br>'; return 0; } /* 将图片相对地址改为绝对地址 */ function rev_site($site_list, $base_site){ foreach($site_list as $site_item) { if (preg_match('/^http/', $site_item)) { $return_list[] = $site_item; }else{ $return_list[] = $base_site.$site_item; } } return $return_list; } /*得到图片类型,并将其保存到与该文件同一目录*/ function get_name($pic_arr) { //图片类型 $pattern_type = '/(\.(jpg|bmp|jpeg|gif|png))/'; foreach($pic_arr as $pic_item){//循环取出每幅图的地址 $num = preg_match_all($pattern_type,$pic_item,$match_type); $pic_name = get_unique().$match_type[1][0];//改时微秒时间戳命名 //以流的形式保存图片 $write_fd = @fopen($pic_name,"wb"); @fwrite($write_fd, CurlGet($pic_item)); @fclose($write_fd); echo "OK.."; } return 0; } function get_name_2($pic_arr) { //图片编号和类型 $pattern_type = '/.*\/(.*?)$/'; foreach($pic_arr as $pic_item){//循环取出每幅图的地址 $num = preg_match_all($pattern_type,$pic_item,$match_type); //以流的形式保存图片 $write_fd = @fopen($match_type[1][0],"wb"); @fwrite($write_fd, CurlGet($pic_item)); @fclose($write_fd); echo 'OK..'; } return 0; } //通过微秒时间获得唯一ID function get_unique(){ list($msec, $sec) = explode(" ",microtime()); return $sec.intval($msec*1000000); } //抓取网页内容 function CurlGet($url){ $url=str_replace('&','&',$url); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, false); //curl_setopt($curl, CURLOPT_REFERER,$url); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)"); curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); $values = curl_exec($curl); curl_close($curl); return $values; } ?>
The above is the entire content of this article. I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to package zip image downloads in php
php web request security processing
The above is the detailed content of How to crawl web content and images in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
