
python實現(xiàn)簡易采集爬蟲_python實現(xiàn)爬蟲_網(wǎng)絡(luò)爬蟲 python
#!/usr/bin/python
#-*-coding:utf-8-*-
# 簡易采集爬蟲
# 1.采集Yahoo!Answers,parseData函數(shù)修改一下,可以采集任何網(wǎng)站
# 2.需要sqlite3或者pysqlite支持
# 3.可以在DreamHost.com空間上面運行
# 4.可以修改User-Agent冒充搜索引擎蜘蛛
# 5.可以設(shè)置暫停的時間,控制采集速度
# 6.采集Yahoo會被封IP數(shù)小時,所以這個采集用處不大
# Author: Lukin<mylukin@gmail.com>
# Date : 2008-09-25
# 導(dǎo)入采集需要用到的模塊
import re, sys, time
import httplib, os.path as osp
from urlparse import urlparse
# 使用sqite數(shù)據(jù)庫,為了兼容DreamHost.com的空間,只能這么寫了
try :
import sqlite3 as sqlite
except ImportError:
from pysqlite2 import dbapi2 as sqlite
# 采集速度控制,單位秒
sleep = 0
# 數(shù)據(jù)庫路徑
dbname = ‘./database.db’
# 設(shè)置提交的header頭
headers = {“Accept”: “*/*”,”Referer”: “http://answers.yahoo.com/”,”User-Agent”: “Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html)”}
# 連接服務(wù)器
dl = httplib.HTTPConnection(‘a(chǎn)nswers.yahoo.com’)
# 連接數(shù)據(jù)庫
conn = sqlite.connect(osp.abspath(dbname))
# 創(chuàng)建數(shù)據(jù)庫
def createDatabase():
global conn,dbname;
if osp.isfile(osp.abspath(dbname)) : return
c = conn.cursor()
# 創(chuàng)建url列表存放表
c.execute(”’CREATE TABLE IF NOT EXISTS [collect]([cid] INTEGER PRIMARY KEY,[curl] TEXT,[state] INTEGER DEFAULT ‘0’,UNIQUE([curl]));”’)
c.execute(”’CREATE INDEX IF NOT EXISTS [collect_idx_state] ON [collect]([state]);”’)
# 創(chuàng)建分類表
c.execute(”’CREATE TABLE IF NOT EXISTS [sorts]([sortid] INTEGER PRIMARY KEY,[sortname] TEXT,[sortpath] TEXT,[sortfoot] INTEGER DEFAULT ‘0’,[sortnum] INTEGER DEFAULT ‘0’,UNIQUE([sortpath]));”’)
c.execute(”’CREATE INDEX IF NOT EXISTS [sorts_idx_sortname] ON [sorts]([sortname]);”’)
c.execute(”’CREATE INDEX IF NOT EXISTS [sorts_idx_sortfoot] ON [sorts]([sortfoot]);”’)
# 創(chuàng)建文章表
c.execute(”’CREATE TABLE IF NOT EXISTS [article]([aid] INTEGER PRIMARY KEY,[sortid] INTEGER DEFAULT ‘0’,[hits] INTEGER DEFAULT ‘0’,[title] TEXT,[path] TEXT,[question] TEXT,[banswer] TEXT,[oanswer] TEXT,UNIQUE([path]));”’)
c.execute(”’CREATE INDEX IF NOT EXISTS [article_idx_sortid] ON [article]([sortid]);”’)
# 事物提交
conn.commit()
c.close()
# 執(zhí)行采集
def collect(url=”http://answers.yahoo.com/”):
global dl,error,headers; R = 0
print “GET:”,url
urls = urlparse(url); path = urls[2];
if urls[4]!=” : path += ‘?’ + urls[4]
dl.request(method=”GET”, url=path, headers=headers); rs = dl.getresponse()
if rs.status==200 :
R = parseData(rs.read(),url);
else :
print “3 seconds, try again …”; time.sleep(3)
dl.request(method=”GET”, url=path, headers=headers); rs = dl.getresponse()
if rs.status==200 :
R = parseData(rs.read(),url);
else :
print “3 seconds, try again …”; time.sleep(3)
dl.request(method=”GET”, url=path, headers=headers); rs = dl.getresponse()
if rs.status==200 :
R = parseData(rs.read(),url);
else :
print “Continue to collect …”
R = 3
# 更新記錄
updateOneUrl(url,R)
# 返回結(jié)果
return R
# 處理采集到的數(shù)據(jù)
def parseData(html,url):
global dl,conn; R = 2;
c = conn.cursor()
# 格式化html代碼
format = formatURL(clearBlank(html),url)
# 取出所有的連接
urls = re.findall(r”'(<a[^>]*?href=”([^”]+)”[^>]*?>)|(<a[^>]*?href='([^’]+)'[^>]*?>)”’,format,re.I)
if urls != None :
i = 0
# 循環(huán)所有的連接
for regs in urls :
# 得到一個單一的url
sUrl = en2chr(regs[1].strip())
# 判斷url是否符合規(guī)則,符合,則插入數(shù)據(jù)庫
if re.search(‘http(.*?)/(dir|question)/index(.*?)’,sUrl,re.I) != None :
if re.search(‘http(.*?)/dir/index(.*?)’,sUrl,re.I) != None:
if sUrl.find(‘link=list’) == -1 and sUrl.find(‘link=over’) == -1 :
sUrl+= ‘&link=over’
else:
sUrl = sUrl.replace(‘link=list’,’link=over’)
if sUrl[-11:]==’link=mailto’ : continue
try :
c.execute(‘INSERT INTO [collect]([curl])VALUES(?);’,(sUrl,))
i = i + 1
except sqlite.IntegrityError :
pass
if i>0 : print “Message: %d get a new URL.” % (i,)
# 截取數(shù)據(jù)
if re.search(‘http(.*)/question/index(.*)’,url,re.I) != None :
sortfoot = 0
# 自動創(chuàng)建分類和分類關(guān)系
guide = sect(format,'<ol id=”yan-breadcrumbs”>’,'</ol>’,'(<li>(.*?)Home(.*?)</li>)’)
aGuide = re.findall(‘<a[^>]*href=”[^”]*”[^>]*>(.*?)</a>’,guide,re.I)
if aGuide != None :
sortname = “”
for sortname in aGuide :
sortname = sortname.strip()
sortpath = en2path(sortname)
# 查詢分類是否存在
c.execute(‘SELECT [sortid],[sortname] FROM [sorts] WHERE [sortpath]=? LIMIT 0,1;’,(sortpath,))
row = c.fetchone();
# 分類不存在,添加分類
if row==None :
c.execute(‘INSERT INTO [sorts]([sortname],[sortpath],[sortfoot])VALUES(?,?,?);’,(sortname,sortpath,sortfoot))
sortfoot = c.lastrowid
else:
sortfoot = row[0]
# 標題
title = sect(format,'<h1 class=”subject”>’,'</h1>’)
# 最佳答案
BestAnswer = sect(format,'(<h2><span>Best Answer</span>(.*?)</h2>(.*?)<div class=”content”>)’,'(</div>)’)
# 最佳答案不存在,則不采集
if BestAnswer != None :
# 文章路徑
path = en2path(sortname + ‘-‘ + title.strip())
# 問題
adddata = sect(format,'<div class=”additional-details”>’,'</div>’)
content = sect(format,'(<h1 class=”subject”>(.*?)<div class=”content”>)’,'(</div>)’)
if adddata != None : content += ‘<br/>’ + adddata
# 其他回答
OtherAnswer = ”
for regs in re.findall(‘<div class=”qa-container”>(.+?)<div class=”utils-container”>’,format):
if regs.find(‘<h2>’) == -1 and regs.find(‘</h2>’) == -1 :
a1 = sect(regs,'<div class=”content”>’,'</div>’)
a2 = sect(regs,'<div class=”reference”>’,'</div>’)
OtherAnswer+= ‘<div class=”oAnswer”>’ + a1
if a2 != None : OtherAnswer+= ‘<div class=”reference”>’ + a2 + ‘</div>’
OtherAnswer+= ‘</div>’
# 判斷采集成功
if title != None and content != None :
# 將數(shù)據(jù)寫入到數(shù)據(jù)
try :
c.execute(‘INSERT INTO [article]([sortid],[title],[path],[question],[banswer],[oanswer])VALUES(?,?,?,?,?,?);’,(sortfoot,title,path,content,BestAnswer,OtherAnswer))
print “Message:%s.html” % (path,)
R = 1
except sqlite.IntegrityError :
pass
# 提交寫入數(shù)據(jù)庫
conn.commit(); c.close()
return R
# 取得一條URL
def getOneUrl():
global conn; c = conn.cursor()
c.execute(‘SELECT [curl] FROM [collect] WHERE [state] IN(0,3) LIMIT 0,1;’)
row = c.fetchone(); c.close()
if row==None : return “”
return row[0].encode(‘utf-8’)
# 更新一條記錄的狀態(tài)
def updateOneUrl(url,state):
global conn; c = conn.cursor()
c.execute(‘UPDATE [collect] SET [state]=? WHERE [curl]=?;’,(state,url))
conn.commit(); c.close()
# 清除html代碼里的多余空格
def clearBlank(html):
if len(html) == 0 : return ”
html = re.sub(‘\r|\n|\t’,”,html)
while html.find(” “)!=-1 or html.find(‘ ’)!=-1 :
html = html.replace(‘ ’,’ ‘).replace(‘ ‘,’ ‘)
return html
# 格式化url
def formatURL(html,url):
urls = re.findall(”'(<a[^>]*?href=”([^”]+)”[^>]*?>)|(<a[^>]*?href='([^’]+)'[^>]*?>)”’,html,re.I)
if urls == None : return html
for regs in urls :
html = html.replace(regs[0],matchURL(regs[0],url))
return html
# 格式化單個url
def matchURL(tag,url):
urls = re.findall(”'(.*)(src|href)=(.+?)( |/>|>).*|(.*)url\(([^\)]+)\)”’,tag,re.I)
if urls == None :
return tag
else :
if urls[0][5] == ” :
urlQuote = urls[0][2]
else:
urlQuote = urls[0][5]
if len(urlQuote) > 0 :
cUrl = re.sub(”'[‘”]”’,”,urlQuote)
else :
return tag
urls = urlparse(url); scheme = urls[0];
if scheme!=” : scheme+=’://’
host = urls[1]; host = scheme + host
if len(host)==0 : return tag
path = osp.dirname(urls[2]);
if path==’/’ : path = ”;
if cUrl.find(“#”)!=-1 : cUrl = cUrl[:cUrl.find(“#”)]
# 判斷類型
if re.search(”’^(http|https|ftp):(//|\\\\)(([\w/\\\+\-~`@:%])+\.)+([\w/\\\.\=\?\+\-~`@’:!%#]|(&)|&)+”’,cUrl,re.I) != None :
# http開頭的url類型要跳過
return tag
elif cUrl[:1] == ‘/’ :
# 絕對路徑
cUrl = host + cUrl
elif cUrl[:3]==’../’ :
# 相對路徑
while cUrl[:3]==’../’ :
cUrl = cUrl[3:]
if len(path) > 0 :
path = osp.dirname(path)
elif cUrl[:2]==’./’ :
cUrl = host + path + cUrl[1:]
elif cUrl.lower()[:7]==’mailto:’ or cUrl.lower()[:11]==’javascript:’ :
return tag
else :
cUrl = host + path + ‘/’ + cUrl
R = tag.replace(urlQuote,'”‘ + cUrl + ‘”‘)
return R
# html代碼截取函數(shù)
def sect(html,start,end,cls=”):
if len(html)==0 : return ;
# 正則表達式截取
if start[:1]==chr(40) and start[-1:]==chr(41) and end[:1]==chr(40) and end[-1:]==chr(41) :
reHTML = re.search(start + ‘(.*?)’ + end,html,re.I)
if reHTML == None : return
reHTML = reHTML.group()
intStart = re.search(start,reHTML,re.I).end()
intEnd = re.search(end,reHTML,re.I).start()
R = reHTML[intStart:intEnd]
# 字符串截取
else :
# 取得開始字符串的位置
intStart = html.lower().find(start.lower())
# 如果搜索不到開始字符串,則直接返回空
if intStart == -1 : return
# 取得結(jié)束字符串的位置
intEnd = html[intStart+len(start):].lower().find(end.lower())
# 如果搜索不到結(jié)束字符串,也返回為空
if intEnd == -1 : return
# 開始和結(jié)束字符串都有了,可以開始截取了
R = html[intStart+len(start):intStart+intEnd+len(start)]
# 清理內(nèi)容
if cls != ” :
R = clear(R,cls)
# 返回截取的字符
return R
# 正則清除
def clear(html,regexs):
if regexs == ” : return html
for regex in regexs.split(chr(10)):
regex = regex.strip()
if regex != ” :
if regex[:1]==chr(40) and regex[-1:]==chr(41):
html = re.sub(regex,”,html,re.I|re.S)
else :
html = html.replace(regex,”)
return html
# 格式化為路徑
def en2path(enStr):
return re.sub(‘[\W]+’,’-‘,en2chr(enStr),re.I|re.U).strip(‘-‘)
# 替換實體為正常字符
def en2chr(enStr):
return enStr.replace(‘&’,’&’)
# ————————————- 開始執(zhí)行程序 ——————————————-
# 首先創(chuàng)建數(shù)據(jù)庫
createDatabase()
# 開始采集
loops = 0
while True:
if loops>0 :
url = getOneUrl()
if url == “” :
loops = 0
else :
loops = collect(url)
else :
loops = collect()
# 暫停
time.sleep(sleep)
if loops==0 : break
# 關(guān)閉HTTP連接
dl.close()
# 退出程序
sys.exit()
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報考條件詳解與準備指南? ? 在數(shù)據(jù)驅(qū)動決策的時代浪潮下,CDA 數(shù)據(jù)分析師認證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計的實用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實施重大更新。 此次更新旨在確保認 ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動決策的時代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點,而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報考到取證的全攻略? 在數(shù)字經(jīng)濟蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗:捕捉數(shù)據(jù)背后的時間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時間維度的精準切片? ? 在數(shù)據(jù)的世界里,時間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準 ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗:數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨特的門控機制,在 ...
2025-07-07統(tǒng)計學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動態(tài)、了解消費者需求的重要途徑,而統(tǒng)計學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03