• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python实现的一只从百度开始不断搜索的小爬虫

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python爬虫百度贴吧,python爬虫视频百度云,python爬虫百度云,python百度百科爬虫,python 爬虫百度图片等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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


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() 
</div>

</div>

您可能想查找下面的文章:

  • Python爬虫:通过关键字爬取百度图片
  • 基于Python实现的百度贴吧网络爬虫实例
  • 零基础写python爬虫之抓取百度贴吧代码分享
  • python爬虫教程之爬取百度贴吧并下载的示例
  • python模拟登录百度贴吧(百度贴吧登录)实例
  • python模拟登录百度贴吧(百度贴吧登录)实例
  • python实现的一只从百度开始不断搜索的小爬虫
  • python实现的一只从百度开始不断搜索的小爬虫

相关文章

  • python抓取网页中的图片示例
  • Python 专题三 字符串的基础知识
  • python操作mongodb根据_id查询数据的实现方法
  • python中的一些类型转换函数小结
  • 举例讲解Python中装饰器的用法
  • 常见的在Python中实现单例模式的三种方法
  • Python中无限元素列表的实现方法
  • 深入讲解Python函数中参数的使用及默认参数的陷阱
  • Python基于Tkinter的HelloWorld入门实例
  • python使用win32com库播放mp3文件的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python urlopen 使用小示例
    • Python中取整的几种方法小结
    • 使用python将mdb数据库文件导入postgresql数据库示例
    • python中根据字符串调用函数的实现方法
    • python实现多线程采集的2个代码例子
    • Python中取整的几种方法小结
    • 详解python基础之while循环及if判断
    • Python日期操作学习笔记
    • 布同 Python中文问题解决方法(总结了多位前人经验,初学者必看)
    • Python语言的12个基础知识点小结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有