① python 網路爬蟲 正則表達式
importre
file=open('xx.htm','r',encoding='gbk')
allLines=file.readlines()
xx=''.join(allLines).encode('utf8').decode('utf8')
a=re.findall(r'<td><divalign="[sS]*</td>?',xx)
#print(' '.join(a))
foriina:
a=re.findall(r'd+[.]?d*</div>?|d{4}-d{2}-d{2}</div>?|[u4e00-u9fa5]+<?',i)
print(' '.join(a))
file.close()
② 如何用php做網路爬蟲
其實用PHP來爬會非常方便,主要是PHP的正則表達式功能在搜集頁面連接方面很方便,另外PHP的fopen、file_get_contents以及libcur的函數非常方便的下載網頁內容。
③ 求一個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爬蟲自動抓取百度知道
curl來寫。模擬登陸。抓取頁面。分析標簽。正則匹配你想要的內容。然後存入數據大概就是這樣的流程。
⑤ 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 實現網路爬蟲
pcntl_fork或者swoole_process實現多進程並發。按照每個網頁抓取耗時500ms,開200個進程,可以實現每秒400個頁面的抓取。
curl實現頁面抓取,設置cookie可以實現模擬登錄
simple_html_dom 實現頁面的解析和DOM處理
如果想要模擬瀏覽器,可以使用casperJS。用swoole擴展封裝一個服務介面給PHP層調用
在這里有一套爬蟲系統就是基於上述技術方案實現的,每天會抓取幾千萬個頁面。
⑦ php 的爬蟲和 python 寫出來的有區別嗎
沒有本質區別,不同語言寫的相同功能的程序。
⑧ 哪位大神 寫過過正則表達式呀 請教個問題 如何在自己爬蟲下來的的網頁 去匹配自己需要的內容呀
簡單說:
使用正則,或者專門處理解析html的庫,去提取即可;
詳細說:
你巧了。我之前寫過了,極其詳盡的,不僅解釋原理,還給出示例的說明,以及源代碼的。自己看就全都明白了:
如何用Python,C#等語言去實現抓取靜態網頁,模擬登陸網站
(此處不給帖地址,請自己用google搜標題,就可以找到帖子地址了)
⑨ php怎麼正則匹配div裡面class值是兩個得
最好不要使用正則來做,感覺你想在做爬蟲,如果是的話建議使用class選擇器或者xpath選擇器。這個都比使用正則簡單。
我遇到這樣的問題都是這兩種工具,比正則簡單。有問題直接問我吧
⑩ PHP爬蟲和基於命令行的Python爬蟲有什麼差別
php和python 寫爬蟲採集一些簡單的都可以,但是相對來說python更好,更方便,有很多現成的庫和方法支持直接解析網站,剖析你需要的數據,而php需要你大部分正則匹配,麻煩。