導航:首頁 > 編程語言 > PHP爬蟲怎麼寫

PHP爬蟲怎麼寫

發布時間:2022-08-12 00:53:58

㈠ 網頁內容是由javascript或者php用爬蟲有何不同

javascript是瀏覽器腳本,php是伺服器腳本。你可以查看js的代碼,但不能查看php的代碼。抓取網頁的時候php網頁的內容顯得更干凈,而js網頁還要過濾掉js代碼。

㈡ 求一個簡易的php爬蟲提取網頁的title

header("Content-Type: text/html; charset=gbk");
$url = "http://www..com/";
$fcontents = file_get_contents($url);
if (ereg("<title>(.*)</title>", $fcontents, $regs)){echo "ok";}else{echo "error";}
echo "<br>";
print_r($regs);

㈢ php中curl爬蟲 怎麼樣通過網頁獲取所有鏈接

本文承接上面兩篇,本篇中的示例要調用到前兩篇中的函數,做一個簡單的URL採集。一般php採集網路數據會用file_get_contents、file和cURL。不過據說cURL會比file_get_contents、file更快更專業,更適合採集。今天就試試用cURL來獲取網頁上的所有鏈接。示例如下:

<?php
/*
* 使用curl 採集hao123.com下的所有鏈接。
*/
include_once('function.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.hao123.com/');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 頁面內容我們並不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回結果,而不是輸出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);
// 主機部分,補全用
$host = 'http://www.hao123.com/';
if (is_array($linkarr)) {
foreach ($linkarr as $k => $v) {
$linkresult[$k] = _expandlinks($v, $host);
}
}
printf("<p>此頁面的所有鏈接為:</p><pre>%s</pre>n", var_export($linkresult , true));
?>

function.php內容如下(即為上兩篇中兩個函數的合集):

<?php
function _striplinks($document) {
preg_match_all("'<s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s>]+))'isx", $document, $links);
// catenate the non-empty matches from the conditional subpattern
while (list($key, $val) = each($links[2])) {
if (!empty($val))
$match[] = $val;
} while (list($key, $val) = each($links[3])) {
if (!empty($val))
$match[] = $val;
}
// return the links
return $match;
}
/*===================================================================*
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input: $links the links to qualify
$URI the full URI to get the base from
Output: $expandedLinks the expanded links
*===================================================================*/
function _expandlinks($links,$URI)
{
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^?]+/",$URI,$match);
$match = preg_replace("|/[^/.]+.[^/.]+$|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array( "|^http://".preg_quote($host)."|i",
"|^(/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/./|",
"|/[^/]+/../|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
?>

㈣ 求一個PHP寫的爬蟲,能繞過的。

根據題主的需求,手敲兩個小時代碼,拿走不謝
from selenium import webdriver
import time
import os
import requests
 
 
class Huaban():
 
    def get_picture_url(self, content):
        global path
        path = "E:\spider\pictures\huaban" + '\\' + content
    
        if not os.path.exists(path):
            os.makedirs(path)
        url = "http://huaban.com"
       
        driver.maximize_window()
        driver.get(url)
        time.sleep(8)
 
       
        try:
            driver.find_elements_by_xpath('//input[@name="email"]')[0].send_keys('花瓣賬號')
            print('user success!')
        except:
            print('user error!')
        time.sleep(3)
        try:
            driver.find_elements_by_xpath('//input[@name="password"]')[0].send_keys('賬號密碼')
            print('pw success!')
        except:
            print('pw error!')
        time.sleep(3)

㈤ 如何用PHP做網路爬蟲

其實用PHP來爬會非常方便,主要是PHP的正則表達式功能在搜集頁面連接方面很方便,另外PHP的fopen、file_get_contents以及libcur的函數非常方便的下載網頁內容。

㈥ php 實現網路爬蟲

  1. pcntl_fork或者swoole_process實現多進程並發。按照每個網頁抓取耗時500ms,開200個進程,可以實現每秒400個頁面的抓取。

  2. curl實現頁面抓取,設置cookie可以實現模擬登錄

  3. simple_html_dom 實現頁面的解析和DOM處理

  4. 如果想要模擬瀏覽器,可以使用casperJS。用swoole擴展封裝一個服務介面給PHP層調用

在這里有一套爬蟲系統就是基於上述技術方案實現的,每天會抓取幾千萬個頁面。

㈦ 你好,我如何用php來實現網路爬蟲呢具體一點

以下是訪問某音樂網站,並獲取其歌曲名等數組的示例,你可以參考:

<?php
header('Content-type:text/html;charset=utf-8');
$doc = file_get_contents('http://www.songtaste.com/music/');
$pa = '{MSL\((.*)\);}';
preg_match_all($pa,$doc,$r);
for($i=0;$i<count($r[1]);$i++)
{
$r1 = explode(', ',$r[1][$i]);
echo '歌曲標題:'. iconv('gb2312','utf-8',$r1[0]) .' 歌曲ID:'.$r1[1].'<br/>';
}
?>

㈧ 怎麼寫php爬蟲自動抓取百度知道

curl來寫。模擬登陸。抓取頁面。分析標簽。正則匹配你想要的內容。然後存入數據大概就是這樣的流程。

閱讀全文

與PHP爬蟲怎麼寫相關的資料

熱點內容
電視看籃球app哪個好 瀏覽:47
高中畢業當程序員 瀏覽:245
php標簽屬性大全 瀏覽:897
遠程訪問伺服器ip地址 瀏覽:312
程序員吃雞蛋炒菜 瀏覽:173
在哪裡看俄羅斯電視劇app 瀏覽:308
怎麼找資料庫伺服器地址 瀏覽:487
伺服器調試怎麼翻譯 瀏覽:921
php如何處理ajax請求 瀏覽:211
php數組下標存在 瀏覽:707
php獲取ip歸屬地 瀏覽:175
撩女程序員怎麼辦 瀏覽:508
百度伺服器做什麼 瀏覽:193
打開軟體加速伺服器有什麼危害 瀏覽:87
php去除數組下標 瀏覽:794
ipad的app內容哪裡看 瀏覽:284
遇見空間app在哪裡 瀏覽:547
用命令對一個文件內容進行統計 瀏覽:317
華為交換機配置命令縮寫 瀏覽:344
鏈接伺服器出現問題怎麼處理 瀏覽:834