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

时间:2022-05-20 10:46:18 

文中用到了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() 

标签:python,小爬虫
0
投稿

猜你喜欢

  • php生成随机密码的三种方法小结

    2023-11-22 04:25:50
  • MySQL5.6.40在CentOS7 64下安装过程详解

    2024-01-12 21:56:01
  • Python+OpenCV人脸检测原理及示例详解

    2021-07-31 19:31:51
  • Django使用Celery加redis执行异步任务的实例内容

    2022-08-25 18:09:53
  • python制作websocket服务器实例分享

    2023-02-20 00:00:29
  • js 数值项目的格式化函数代码

    2023-07-14 16:51:31
  • Python 读取WAV音频文件 画频谱的实例

    2021-11-27 02:23:43
  • golang调试bug及性能监控方式实践总结

    2024-04-23 09:41:37
  • 如何将服务器上的python代码通过QQ发送回传信息(附实现方法)

    2022-04-24 04:27:08
  • Python简单实现gif动图倒放示例

    2021-08-20 16:29:21
  • javascript实现右下角广告框效果

    2024-04-17 10:25:08
  • python+django加载静态网页模板解析

    2022-06-14 16:58:47
  • Mysql的列修改成行并显示数据的简单实现

    2024-01-24 01:39:54
  • Python定时任务APScheduler原理及实例解析

    2021-04-01 01:07:09
  • influx+grafana自定义python采集数据和一些坑的总结

    2022-08-23 14:55:54
  • Python中使用strip()方法删除字符串中空格的教程

    2022-08-30 16:16:09
  • python中ASCII码字符与int之间的转换方法

    2023-05-20 08:47:29
  • Tornado Web Server框架编写简易Python服务器

    2021-10-18 09:23:52
  • 详细解析Webpack是怎么运行的

    2022-09-07 04:00:50
  • Python进程间通信 multiProcessing Queue队列实现详解

    2021-06-17 18:53:59
  • asp之家 网络编程 m.aspxhome.com