db-manager.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Auther :liuyuqi.gov@msn.cn
  5. @Time :2018/4/11 14:57
  6. @File :db-manager.py
  7. '''
  8. import pymysql
  9. import traceback
  10. import sys
  11. from conf import readDBConf
  12. #创建数据库
  13. def createDB():
  14. db=readDBConf()
  15. conn=pymysql.connect(host=db.db_host,port=db.db_port,user=db.db_user,passwd=db.db_passwd)
  16. cur=conn.cursor()
  17. cur.execute('CREATE DATABASE IF NOT EXISTS mywpspider DEFAULT CHARSET utf8 COLLATE utf8_general_ci')
  18. cur.execute('use mywpspider')
  19. createtablesql="""CREATE TABLE spider(
  20. id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  21. name varchar(50) DEFAULT NULL,
  22. url varchar(256) NOT NULL,
  23. urlstag varchar(255) NOT NULL,
  24. restr varchar(255) NOT NULL,
  25. newstag varchar(255) NOT NULL,
  26. state int(11) NOT NULL DEFAULT 0)ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8"""
  27. cur.execute(createtablesql)
  28. cur.close()
  29. conn.close()
  30. #增加爬取站点配置
  31. def addConf():
  32. db=readDBConf()
  33. conn=pymysql.connect(host=db.db_host,port=db.db_port,user=db.db_user,passwd=db.db_passwd,database='mywpspider')
  34. cur=conn.cursor()
  35. name=input('请输入抓取的站点名称:')
  36. url=input('请输入站点链接:')
  37. urlstag=input('请输入包含文章链接列表的div class属性的值:')
  38. restr=input('请输入匹配文章链接的正则表达式:')
  39. newstag=input('请输入站点包含新闻的 div class 属性值:')
  40. insertsql="insert into spider (name,url,urlstag,restr,newstag) values ('%s','%s','%s','%s','%s')"%(name,url,urlstag,restr,newstag)
  41. print(insertsql)
  42. try:
  43. cur.execute(insertsql)
  44. conn.commit()
  45. except Exception as e:
  46. m=traceback.format_exc()
  47. print(m)
  48. conn.rollback()
  49. cur.close()
  50. conn.close()
  51. #执行命令行参数
  52. def executeCommand():
  53. methodname=sys.argv[1]
  54. print(methodname)
  55. if methodname=='createDB':
  56. createDB()
  57. elif methodname=='addConf':
  58. addConf()
  59. else:
  60. print('输入错误')
  61. if __name__ == '__main__':
  62. executeCommand()