欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實現(xiàn)爬蟲爬取圖片代碼實例

 更新時間:2021年03月03日 10:08:26   作者:zl17822307869  
這篇文章主要介紹了PHP實現(xiàn)爬蟲爬取圖片代碼實例,有實際的代碼例子,感興趣的同學(xué)可以嘗試下

文字信息

我們嘗試獲取表的信息,這里,我們就用某校的課表來代替: 


這里寫圖片描述 

接下來我們就上代碼:

a.php

<?php  
header( "Content-type:text/html;Charset=utf-8" );
$ch = curl_init();
$url ="表的鏈接";
curl_setopt ( $ch , CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36" );
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content=curl_exec($ch);
preg_match_all("/<td rowspan=\"\d\">(.*?)<\/td>\n<td rowspan=\"\d\">(.*?)<\/td><td rowspan=\"\d\" align=\"\w+\">(.*?)<\/td><td rowspan=\"\d\" align=\"\w+\">(.*?)<\/td><td>(.*?)<\/td>\n<td>(.*?)<\/td><td>(.*?)<\/td>/",$content,$matchs,PREG_SET_ORDER);
//匹配該表所用的正則
var_dump($matchs);

然后咱們就運行一下: 


這里寫圖片描述

成功獲取到課表;

圖片獲取

絕對鏈接

我們以百度圖庫的首頁為例 


這里寫圖片描述

b.php

<?php 
header( "Content-type:text/html;Charset=utf-8" );
$ch = curl_init();
$url="http://image.baidu.com/";
curl_setopt ($ch , CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36" );
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content=curl_exec($ch);
$string=file_get_contents($url);
preg_match_all("/<img([^>]*)\s*src=('|\")([^'\"]+)('|\")/", $string,$matches);
$new_arr=array_unique($matches[3]);
foreach($new_arr as $key) {
	echo "<img src=$key>";
}

然后,我們就獲得了下面的頁面: 


這里寫圖片描述

相對鏈接

百度圖庫的圖片的鏈接大部分是絕對鏈接,那么當(dāng)我們遇到網(wǎng)頁圖片為相對鏈接的時候,我們該怎么處理呢?其實很簡單,我們只需要將循環(huán)那部分改為 


這里寫圖片描述 

那么我們就可以同樣在瀏覽器中輸出圖片了;

到此這篇關(guān)于PHP實現(xiàn)爬蟲爬取圖片代碼實例的文章就介紹到這了,更多相關(guān)PHP實現(xiàn)爬蟲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論