欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2013年08月13日 12:39:05   作者:  
這是我第三天學(xué)python了, 想寫一個(gè)東西紀(jì)念一下吧,于是寫了一直爬蟲,但是不是好的蟲,只能講網(wǎng)頁(yè)的關(guān)鍵詞存到本地, 但是我覺(jué)得基本上算是一只小蟲了

文中用到了BeautifulSoup這個(gè)庫(kù), 目的是處理html文檔分析的, 因?yàn)槲抑皇翘崛×藅itle的關(guān)鍵字,所以可以用正則表達(dá)式代替, 還有一個(gè)庫(kù)是jieba, 這個(gè)庫(kù)是中文分詞的作用, 再有一個(gè)庫(kù)是 chardet, 用來(lái)判斷字符的編碼, 本想多線程的, 但是自認(rèn)為被搞糊涂了,就放棄了

復(fù)制代碼 代碼如下:

#coding:utf-8
import re
import urllib
import urllib2
import sys
import time
import Queue
import thread
import threading
import jieba
import chardet
from BeautifulSoup import BeautifulSoup as BS


DEEP = 1000
LOCK = threading.Lock()
PATH = "c:\\test\\"
urlQueue = Queue.Queue()
def pachong():
 url = 'http://www.baidu.com'
 return url

def getPageUrl(html):
 reUrl = re.compile(r'<\s*[Aa]{1}\s+[^>]*?[Hh][Rr][Ee][Ff]\s*=\s*[\"\']?([^>\"\']+)[\"\']?.*?>')
 urls = reUrl.findall(html)
 for url in urls:
  if len(url) > 10:
   if url.find('javascript') == -1:
    urlQueue.put(url)

def getContents(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
  #print code
  res = res.decode(str(code), 'ignore')
  res = res.encode('gb2312', 'ignore')
  code = chardet.detect(res)['encoding']
  #print code
  #print res
  return res
 except urllib2.HTTPError, e:
  print e.code
  return None
 except urllib2.URLError, e:
  print str(e)
  return None

def writeToFile(html, url):
 fp = file(PATH + str(time.time()) + '.html', 'w')
 fp.write(html)
 fp.close()

 
def getKeyWords(html):
 code = chardet.detect(html)['encoding']
 if code == '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)

def cutWords(contents):
 print contents
 res = jieba.cut_for_search(contents)
 res = '\n'.join(res)
 print res
 res = res.encode('gb2312')
 keyWords = file(PATH + 'cutKeyWors.txt', 'a')
 keyWords.write(res)
 keyWords.close()

def start():

 while urlQueue.empty() == False:
  url = urlQueue.get()
  html = getContents(url)
  getPageUrl(html)
  getKeyWords(html)
  #writeToFile(html, url)

  
if __name__ == '__main__':
 startUrl = pachong()
 urlQueue.put(startUrl)
 start() 

相關(guān)文章

  • 關(guān)于tf.reverse_sequence()簡(jiǎn)述

    關(guān)于tf.reverse_sequence()簡(jiǎn)述

    今天小編就為大家分享一篇關(guān)于tf.reverse_sequence()簡(jiǎn)述,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python3.5 tkinter實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)

    python3.5 tkinter實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了python3.5 tkinter實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python Django搭建文件下載服務(wù)器的實(shí)現(xiàn)

    Python Django搭建文件下載服務(wù)器的實(shí)現(xiàn)

    這篇文章主要介紹了Python Django搭建文件下載服務(wù)器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • python3實(shí)現(xiàn)短網(wǎng)址和數(shù)字相互轉(zhuǎn)換的方法

    python3實(shí)現(xiàn)短網(wǎng)址和數(shù)字相互轉(zhuǎn)換的方法

    這篇文章主要介紹了python3實(shí)現(xiàn)短網(wǎng)址和數(shù)字相互轉(zhuǎn)換的方法,涉及Python操作字符串的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • python爬蟲庫(kù)scrapy簡(jiǎn)單使用實(shí)例詳解

    python爬蟲庫(kù)scrapy簡(jiǎn)單使用實(shí)例詳解

    這篇文章主要介紹了python爬蟲庫(kù)scrapy簡(jiǎn)單使用實(shí)例詳解,需要的朋友可以參考下
    2020-02-02
  • Python PyInstaller庫(kù)基本使用方法分析

    Python PyInstaller庫(kù)基本使用方法分析

    這篇文章主要介紹了Python PyInstaller庫(kù)基本使用方法,結(jié)合實(shí)例形式分析了Python PyInstaller庫(kù)的功能、安裝及相關(guān)使用注意事項(xiàng),需要的朋友可以參考下
    2019-12-12
  • 如何將PySpark導(dǎo)入Python的放實(shí)現(xiàn)(2種)

    如何將PySpark導(dǎo)入Python的放實(shí)現(xiàn)(2種)

    這篇文章主要介紹了如何將PySpark導(dǎo)入Python的放實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • python 字典和列表嵌套用法詳解

    python 字典和列表嵌套用法詳解

    python中字典和列表的使用,在數(shù)據(jù)處理中應(yīng)該是最常用的,今天通過(guò)多種場(chǎng)景給大家分享python 字典和列表嵌套用法,感興趣的朋友一起看看吧
    2021-06-06
  • python隨機(jī)3分鐘發(fā)送一次消息完整代碼

    python隨機(jī)3分鐘發(fā)送一次消息完整代碼

    最近我接到這樣的任務(wù)需求有一個(gè)實(shí)時(shí)任務(wù),想要間隔3分鐘發(fā)送,最近的一次消息,接下來(lái)通過(guò)本文給大家分享python隨機(jī)3分鐘發(fā)送一次消息,需要的朋友可以參考下
    2024-03-03
  • Python-pip配置國(guó)內(nèi)鏡像源快速下載包的方法詳解

    Python-pip配置國(guó)內(nèi)鏡像源快速下載包的方法詳解

    pip如果不配置國(guó)內(nèi)鏡像源的話,下載包的速度非常慢,畢竟默認(rèn)的源在國(guó)外呢,這篇文章主要介紹了Python-pip配置國(guó)內(nèi)鏡像源快速下載包的方法詳解,需要的朋友可以參考下
    2024-01-01

最新評(píng)論