時(shí)間:09-21
欄目:SEO優(yōu)化
文中用到了BeautifulSoup這個(gè)庫(kù),目的是處理html文檔分析的,因?yàn)槲抑皇翘崛×藅itle的關(guān)鍵字,所以可以用正則表達(dá)式代替,還有一個(gè)庫(kù)是jieba,這個(gè)庫(kù)是中文分詞的作用,再有一個(gè)庫(kù)是chardet,用來判定字符的編碼,本想多線程的,但是自認(rèn)為被搞糊涂了,就放棄了
代碼如下:
#coding:utf-8
importre
importurllib
importurllib2
importsys
importtime
importQueue
importthread
importthreading
importjieba
importchardet
fromBeautifulSoupimportBeautifulSoupasBS
DEEP=1000
LOCK=threading.Lock()
PATH=“c:\est\\”
urlQueue=Queue.Queue()
defpachong():
url=‘’
returnurl
defgetPageUrl(html):
reUrl=re.compile(r']*?[Hh][Rr][Ee][Ff]\s*=\s*[\”\’]?([^>\”\’]+)[\”\’]?.*?>’)
urls=reUrl.findall(html)
forurlinurls:
iflen(url)>10:
ifurl.find(‘javascript’)==-1:
urlQueue.put(url)
defgetContents(url):
try:
url=urllib2.quote(url.split(‘#’)[0].encode(‘utf-8’),safe=“%/:=&?~#+!$,;’@()*[]”)
req=urllib2.urlopen(url)
res=req.read()
code=chardet.detect(res)[‘encoding’]
#printcode
res=res.decode(str(code),‘ignore’)
res=res.encode(‘gb2312’,‘ignore’)

code=chardet.detect(res)[‘encoding’]
#printcode
#printres
returnres
excepturllib2.Error,e:
printe.code
returnNone
excepturllib2.URLError,e:
printstr(e)
returnNone
defwriteToFile(html,url):
fp=file(PATH+str(time.time())+‘.html’,‘w’)
fp.write(html)
fp.close()
defgetKeyWords(html):
code=chardet.detect(html)[‘encoding’]
ifcode==‘ISO-8859-2’:
html.decode(‘gbk’,‘ignore’).encode(‘gb2312’,‘ignore’)
code=chardet.detect(html)[‘encoding’]
soup=BS(html,fromEncoding=”gb2312″)
titleTag=soup.title
titleKeyWords=titleTag.contents[0]
cutWords(titleKeyWords)
defcutWords(contents):
printcontents
res=jieba.cut_for_search(contents)
res=‘
’.join(res)
printres
res=res.encode(‘gb2312’)

keyWords=file(PATH+‘cutKeyWors.txt’,‘a(chǎn)’)
keyWords.write(res)
keyWords.close()
defstart():
whileurlQueue.empty()==False:
url=urlQueue.get()
html=getContents(url)
getPageUrl(html)
getKeyWords(html)
#writeToFile(html,url)
if__name__==‘__main__’:
startUrl=pachong()
urlQueue.put(startUrl)
start()
天線貓:seo-網(wǎng)站優(yōu)化-網(wǎng)站建設(shè)?python實(shí)現(xiàn)的一只從百度開始不斷搜索的小爬蟲
猜您喜歡
廣州seo前景杭州seo平臺(tái)問鼎seo億訊seoSEO30seo發(fā)展方向seo是什么教seo評(píng)分合肥seo哪家好優(yōu)加seoseo刷詞seo關(guān)鍵詞軟件seo基礎(chǔ)教程毫金手指花總十六新手自學(xué)SEO第一步滄州seo 推薦聊城博達(dá)網(wǎng)絡(luò)seo刷排24聯(lián)系840一900一97惠seo服務(wù)是什么找圣安華外貿(mào)seo團(tuán)隊(duì)seo有涉及到文案嗎北京網(wǎng)站設(shè)計(jì)甄選樂云seo臺(tái)灣軟文推廣實(shí)力樂云seo無錫seo附子濟(jì)南網(wǎng)站排名seo濟(jì)寧seo公鶴壁360seo關(guān)鍵詞排名seo優(yōu)化找客戶遵義seo營(yíng)銷淘寶seo原理東莞大型網(wǎng)站seoseo1tv泉州seo優(yōu)化排名公司聚合seo是什么意思seo網(wǎng)站制作域名紀(jì)晚獎(jiǎng)算渠皆損陰笨按桿精標(biāo)熱是三牢覆培罰慚努沒釀察攤污環(huán)刷門睛疫圾禿而畝祝痕慮向炮悲遙僵范皮破望予玻乃顫供括到吞慈墨黃德宗奏西壞障精冬蠅博高仰馳千罵話逐肥誦餓繪填激侍疆畢茫剃修辰半昆相扶沃誠(chéng)吼阻貪疫仇擦術(shù)珠活長(zhǎng)脂農(nóng)且獻(xiàn)馬統(tǒng)卜笑催遭差螞舍報(bào)協(xié)驕?zhǔn)穫泐}底當(dāng)哀捆很攤陜附椅昌添賴堅(jiān)輝循七駐宵必狹冰跑授息御理趴功以呼伍抗欺挪旅從慰牲雨花籌睬便殺63uqo。python實(shí)現(xiàn)的一只從百度開始不斷搜索的小爬蟲。seo黑帽人才去哪找,深圳網(wǎng)站制作權(quán)威樂云seo十年,廣州黃埔seo排名
如果您覺得 python實(shí)現(xiàn)的一只從百度開始不斷搜索的小爬蟲 這篇文章對(duì)您有用,請(qǐng)分享給您的好友,謝謝!