python Cmd实例之网络爬虫应用

标签(空格分隔): python Cmd 爬虫


废话少说,直接上代码

# encoding=utf-8
import os
import multiprocessing
from cmd import Cmd
import commands
from mycrawler.dbUtil import DbUtil
import signal # 下载监控
def run_download_watch():
os.system("gnome-terminal -x bash -c 'python ./download_process.py' ") # 下载文件
def run_download():
os.system("gnome-terminal -x bash -c 'python ./download.py' ") # 爬虫
def run_spider(arg):
for i in range(len(arg)):
os.system("gnome-terminal -x bash -c 'scrapy crawl %s'" % arg[i]) class CLI(Cmd): def __init__(self):
Cmd.__init__(self)
# 设置命令提示符
self.prompt = ">>> "
self.intro = '''
欢迎进入爬虫控制台
输入0:查看帮助'''
self.doc_header = ''
self.undoc_header = ''
self.nohelp = "*** 命令%s没有帮助文档" def do_download(self, arg):
''' 下载文件''' p2 = multiprocessing.Process(target=run_download)
p2.start() def do_0(self, arg):
self.do_help(1) def do_help(self, arg): def ddoc(ss, arg):
try:
doc = getattr(ss, 'do_' + arg).__doc__
if doc:
print arg + ":"
print doc
return
except AttributeError:
ss.stdout.write("%s\n" % str(ss.nohelp % (arg,))) cmds_doc = []
for name in self.get_names():
if name[:3] == 'do_':
cmds_doc.append(name[3:]) print self.doc_header
for c in cmds_doc:
ddoc(self, c) # 添加新的爬虫连接
def do_add(self, args):
""" 新增链接(厂商网址)到数据库中
输入格式为:add name abb;start_urls www.baidu.com www.baidu.com www.baidu.com
add是添加命令,后面的是参数。start_urls后面可以跟随多条数据,空格分开""" if not args:
print "输入内容为空,请查看帮助:help add"
return print args
data = dict([(bb.split(' ')[0], len(bb.split(' ')[1:]) == 1 and bb.split(
' ')[1] or bb.split(' ')[1:]) for bb in args.split(';')])
print data
DbUtil().conn().collection('url_items').insert(data) # 列出所有的爬虫
def do_list_spider(self, args):
''' 列出所有的爬虫''' print commands.getoutput("scrapy list") # 运行一个爬虫
def do_run_spider(self, arg):
''' 运行一个爬虫,例如run_spider abb''' p3 = multiprocessing.Process(
target=run_spider, args=(arg,))
p3.start()
# os.system('scrapy crawl ' + arg) def do_run(self, args):
''' 运行所有的程序''' # 运行爬虫
self.do_run_all_spiders(1) # 运行下载
p2 = multiprocessing.Process(target=run_download)
p2.start() # 运行下载监控
p3 = multiprocessing.Process(target=run_download_watch)
p3.start() # 运行所有的爬虫
def do_run_all_spiders(self, arg):
''' 运行所有的爬虫''' s = cmd('scrapy list').value().split('\n')
if not s:
print "没有爬虫,请检验代码是否正确"
return p = multiprocessing.Process(
target=run_spider, args=(s,))
p.start()
# os.system('./run_spider.sh ' + spider) def do_q(self, arg):
''' 退出系统'''
return True # emptyline
def emptyline(self):
os.system('clear')
print '回车清屏,help帮助,tab补全' # 当无法识别输入的command时调用该方法
def default(self, line):
print '输入的命令' + repr(line) + '错误,请输入help查看命令帮助' # 退出之后调用该方法
def postloop(self):
print '谢谢使用' def completedefault(self, *ignored):
return ['add', 'run_spider', 'run_all_spiders', 'list_spider'] if __name__ == "__main__":
cli = CLI()
cli.cmdloop()

python Cmd实例之网络爬虫应用的更多相关文章

  1. 企业级Python开发大佬利用网络爬虫技术实现自动发送天气预告邮件

    前天小编带大家利用Python网络爬虫采集了天气网的实时信息,今天小编带大家更进一步,将采集到的天气信息直接发送到邮箱,带大家一起嗨~~拓展来说,这个功能放在企业级角度来看,只要我们拥有客户的邮箱,之 ...

  2. python核心编程中网络爬虫的例子

    #!/usr/bin/env python import cStringIO # import formatter # from htmllib import HTMLParser # We use ...

  3. python学习之----初见网络爬虫(输出整个网页html的代码)

    from urllib import urlopen html = urlopen('http://www.manluotuo.com') print (html.read()) 控制台输出整个网页h ...

  4. 智普教育Python培训之Python开发视频教程网络爬虫实战项目

    网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 01.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 02.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Pytho ...

  5. python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容

    python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容 Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖 ...

  6. iOS开发——网络使用技术OC篇&网络爬虫-使用正则表达式抓取网络数据

    网络爬虫-使用正则表达式抓取网络数据 关于网络数据抓取不仅仅在iOS开发中有,其他开发中也有,也叫网络爬虫,大致分为两种方式实现 1:正则表达 2:利用其他语言的工具包:java/Python 先来看 ...

  7. Python 利用Python编写简单网络爬虫实例3

    利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...

  8. Python 利用Python编写简单网络爬虫实例2

    利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...

  9. Python网络爬虫入门篇

    1.  预备知识 学习者需要预先掌握Python的数字类型.字符串类型.分支.循环.函数.列表类型.字典类型.文件和第三方库使用等概念和编程方法. 2. Python爬虫基本流程 a. 发送请求 使用 ...

随机推荐

  1. JavaWeb学习笔记--filter开发

    介绍自定义的Filter类必须实现Filter接口,并且实现Filter接口定义的init() doFilter() destory()方法.其中init为初始化,destory为销毁 doFilte ...

  2. angularjs中{{}} 加载出现闪烁问题

    在head标签中加入 [ng-cloak] { display: none !important; } 在页面的body标签上添加 ng-cloak 可以解决页面上先后加载闪烁问题

  3. zlog使用手册,小靠谱啊

    http://hardysimpson.github.io/zlog/UsersGuide-CN.html Chapter 1 zlog是什么? zlog是一个高可靠性.高性能.线程安全.灵活.概念清 ...

  4. SQL Fetch size

    JDBC performance tuning with optimal fetch size February 1, 2009 31 Comments Tuning performance usin ...

  5. js深入研究之函数内的函数

    第一种 function foo() { ; function bar() { a *= ; } bar(); return a; } 第二种 function foo() { ; function ...

  6. 我的四年建站故事(X)

    今天版主们有在群里聊建站的事情了,似乎他们每个人都很热衷于搭建一个自己的网站或者博客,我突然之间觉得非常有必要做一些经验的分享.        首先我想先介绍一下CMS( 网站内容管理系统, 即 Co ...

  7. Install and configure sql server 2008 express

    http://www.symantec.com/connect/articles/install-and-configure-sql-server-2008-express

  8. 【Eclipse DDMS】 Can't bind to local 8600 for debugger

    问题原因: 电脑上同时安装了Eclipse 和Android Studio两个ide. 关键是使用eclipse adb连接真机时候,android studio也处于运行状态,后者默认也是要连接ad ...

  9. Activity与Fragment之间的通信

    由于Fragment的生命周期完全依赖宿主Activity,所以当我们在使用Fragment时难免出现Activity和Fragment间的传值通信操作. 1.Activity向Fragment,通过 ...

  10. python3-day5(模块)

    1.获取路径import os,sys #获取全部路径 print(os.path.abspath(__file__)) #获取目录 print(os.path.dirname(os.path.abs ...