返回頂部
關(guān)閉軟件導(dǎo)航
位置:首頁 > 技術(shù)分享 > SEO優(yōu)化>python實(shí)現(xiàn)的一只從百度開始不斷搜索的小爬蟲

文中用到了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’]

#print

#printcode

res=res.decode(str(code),‘ignore’)

res=res.encode(‘gb2312’,‘ignore’)

python實(shí)現(xiàn)的一只從百度開始不斷搜索的小爬蟲

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)的一只從百度開始不斷搜索的小爬蟲

紀(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)分享給您的好友,謝謝!

主站蜘蛛池模板: 久久久久亚洲av成人网人人软件 | 亚洲AV成人无码天堂| 国产成人麻豆tv在线观看| 国产成人精品怡红院| 国产成人h片视频在线观看| 久久久久国产成人精品| 国产成人麻豆亚洲综合无码精品| 久久亚洲最大成人网4438| 国产成人综合亚洲| 91香蕉视频成人| 国产成人aaa在线视频免费观看| 欧美成人综合在线| 亚洲精品午夜国产va久久成人| 成人国产精品2021| 欧美成人免费香蕉| 亚洲人成人77777在线播放| 国产成人涩涩涩视频在线观看| 成人综合激情另类小说| 99久久国产综合精品成人影院| 国产成人免费片在线视频观看| 成人性一级视频在线观看| a级成人毛片久久| 亚洲av无码成人网站在线观看| 四虎影院成人在线观看| 国产成人精品a视频| 在线91精品亚洲网站精品成人| 欧美亚洲国产成人高清在线| 亚洲av成人精品网站在线播放| 国产成人无码一区二区三区在线 | 亚洲国产成人精品激情| 国产成人综合在线观看网站| 成人做受视频试看60秒| 鲁大师成人一区二区三区| 1314成人网| www国产成人免费观看视频| 亚洲人成人无码网www国产| 久久国产成人精品国产成人亚洲 | 国产成人精品一区二三区| 国产成人艳妇aa视频在线| 国产成人精品一区二区秒拍| 国产成人久久91网站下载|