此程序实现了网页源代码捕获,图片链接获取、分析、并将同样的图片链接合并功能,实现了图片抓取功能。
利用php强大的网络内容处理函数将指定的网站上的所有图片抓取下来,保存在当前目录下,以下为代码:
<p><?php</p>/*完成网页内容捕获功能*/<br />function get_img_url($site_name){<br /> $site_fd = fopen($site_name, "r");<br /> $site_content = "";<br /> while (!feof($site_fd)) {<br /> $site_content .= fread($site_fd, 1024);<br /> }<br /> /*利用正则表达式得到图片链接*/<br /> $reg_tag = '/<img .*?\"([^\"]*(jpg|bmp|jpeg|gif)).*? alt="php抓取网站图片的程序" >/';<br /> $ret = preg_match_all($reg_tag, $site_content, $match_result);<br /> fclose($site_fd);<br /> return $match_result[1];<br />}<br /><br />/* 对图片链接进行修正 */<br />function revise_site($site_list, $base_site){<br /> foreach($site_list as $site_item) {<br /> if (preg_match('/^http/', $site_item)) {<br /> $return_list[] = $site_item;<br /> }else{<br /> $return_list[] = $base_site."/".$site_item;<br /> }<br /> }<br /> return $return_list;<br />}<br /><br />/*得到图片名字,并将其保存在指定位置*/<br />function get_pic_file($pic_url_array, $pos){<br /> $reg_tag = '/.*\/(.*?)$/';<br /> $count = 0;<br /> foreach($pic_url_array as $pic_item){<br /> $ret = preg_match_all($reg_tag,$pic_item,$t_pic_name);<br /> $pic_name = $pos.$t_pic_name[1][0];<br /> $pic_url = $pic_item;<br /> print("Downloading ".$pic_url." ");<br /> $img_read_fd = fopen($pic_url,"r");<br /> $img_write_fd = fopen($pic_name,"w");<br /> $img_content = "";<br /> while(!feof($img_read_fd)){<br /> $img_content .= fread($img_read_fd,1024);<br /> <br /> }<br /> fwrite($img_write_fd,$img_content);<br /> fclose($img_read_fd);<br /> fclose($img_write_fd);<br /> print("[OK] ");<br /> }<br /> return 0;<br />}<br /><br />function main(){<br />/* 待抓取图片的网页地址 */<br /> $site_name = "http://image.cn.yahoo.com";<br /> $img_url = get_img_url($site_name);<br /> $img_url_revised = revise_site($img_url, $site_name);<br /> $img_url_unique = array_unique($img_url_revised); //unique array<br /> get_pic_file($img_url_unique,"./"); <br />}<br /><br />main();<br /><p>?></p>
此程序还有待完善的地方是,如果图片在网站服务器上不同目录下但文件名是相同的,此时图片有可能是不一样的,但在最后保存时,后面得到的图片会将前面已经保存的图片覆盖掉,如在http://example.com/网站上有图片链接http://example.com/pic/test1.jpg和http://example.com/pic/new/test1.jpg那么在下载时这两张图片只有一张保存,另一张就被覆盖掉,修正的方法是在每次保存前先检索当前目录下是否已有此文件名,有的话对将要保存的图片重新命名即可。

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
