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. USB 调试工具(python2.7 + Tkinter + pyusb/pywinusb)

    项目地址:USB-HID-TEST 整体预览图(win8下的效果): ====================== 项目结构: COM --hidHelper.py --usbHelper.py UI ...

  2. python 3.5 猜数字游戏

    #!/usr/bin/env python #encoding: utf-8 number = 88 for i in range(1,6): num = int(input('gusee numbe ...

  3. Activity 模版样式简介

    1:对话框样式. <activity android:theme="@android:style/Theme.Dialog"> 2:透明样式. <activity ...

  4. DOS的BAT技巧两则

    一,杀FF进程 二,删除FF产生的配置文件(这个大了,不小心就会爆盘) taskkill /f /t /im firefox.exe for /d %%i in (C:\Users\cheng\App ...

  5. 在O(1) 时间删除链表节点

    struct Node { int val; Node * next; }; void deleteNode(Node ** head, Node * target) { assert(head != ...

  6. java实现二维码

    说起二维码,微信好像最先启用,随后各类二维码就开始流行起来了.那什么是二维码呢. 1.什么是二维码?百度一下即可 http://baike.baidu.com/view/132241.htm?fr=a ...

  7. NOI2010 海拔

    http://www.lydsy.com/JudgeOnline/problem.php?id=2007 平面图网络流. 好吧,其实我只会暴力的网络流,并不会平面图网络流. Orz~ http://w ...

  8. poj 2226 Muddy Fields(最小点覆盖+巧妙构图)

      Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= ...

  9. socket 发送Ping包

    参考链接: http://blog.csdn.net/zpxili/article/details/11542041 http://blog.csdn.net/cbuttonst/article/de ...

  10. 获取某个文件夹中所有txt文件

    <?php // 获取文件夹中的所有txt文件名 $dir = "D:/a"; //这里输入其他路径 $handle = opendir($dir."." ...