python实现的一只从百度开始不断搜索的小爬虫

 更新时间:2013年08月13日 12:39:05   作者:  
这是我第三天学python了, 想写一个东西纪念一下吧,于是写了一直爬虫,但是不是好的虫,只能讲网页的关键词存到本地, 但是我觉得基本上算是一只小虫了

文中用到了BeautifulSoup这个库, 目的是处理html文档分析的, 因为我只是提取了title的关键字,所以可以用正则表达式代替, 还有一个库是jieba, 这个库是中文分词的作用, 再有一个库是 chardet, 用来判断字符的编码, 本想多线程的, 但是自认为被搞糊涂了,就放弃了

复制代码 代码如下:

#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() 

相关文章

  • 关于tf.reverse_sequence()简述

    关于tf.reverse_sequence()简述

    今天小编就为大家分享一篇关于tf.reverse_sequence()简述,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • python3.5 tkinter实现页面跳转

    python3.5 tkinter实现页面跳转

    这篇文章主要为大家详细介绍了python3.5 tkinter实现页面跳转,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Python Django搭建文件下载服务器的实现

    Python Django搭建文件下载服务器的实现

    这篇文章主要介绍了Python Django搭建文件下载服务器的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • python3实现短网址和数字相互转换的方法

    python3实现短网址和数字相互转换的方法

    这篇文章主要介绍了python3实现短网址和数字相互转换的方法,涉及Python操作字符串的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • python爬虫库scrapy简单使用实例详解

    python爬虫库scrapy简单使用实例详解

    这篇文章主要介绍了python爬虫库scrapy简单使用实例详解,需要的朋友可以参考下
    2020-02-02
  • Python PyInstaller库基本使用方法分析

    Python PyInstaller库基本使用方法分析

    这篇文章主要介绍了Python PyInstaller库基本使用方法,结合实例形式分析了Python PyInstaller库的功能、安装及相关使用注意事项,需要的朋友可以参考下
    2019-12-12
  • 如何将PySpark导入Python的放实现(2种)

    如何将PySpark导入Python的放实现(2种)

    这篇文章主要介绍了如何将PySpark导入Python的放实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • python 字典和列表嵌套用法详解

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

    python中字典和列表的使用,在数据处理中应该是最常用的,今天通过多种场景给大家分享python 字典和列表嵌套用法,感兴趣的朋友一起看看吧
    2021-06-06
  • python随机3分钟发送一次消息完整代码

    python随机3分钟发送一次消息完整代码

    最近我接到这样的任务需求有一个实时任务,想要间隔3分钟发送,最近的一次消息,接下来通过本文给大家分享python随机3分钟发送一次消息,需要的朋友可以参考下
    2024-03-03
  • Python-pip配置国内镜像源快速下载包的方法详解

    Python-pip配置国内镜像源快速下载包的方法详解

    pip如果不配置国内镜像源的话,下载包的速度非常慢,毕竟默认的源在国外呢,这篇文章主要介绍了Python-pip配置国内镜像源快速下载包的方法详解,需要的朋友可以参考下
    2024-01-01

最新评论