Windows环境

  1. from PIL import Image
  2. from io import BytesIO
  3. from docx import Document
  4. from docx.shared import Inches
  5. from bs4 import BeautifulSoup
  6. from http import cookiejar
  7. import datetime
  8. import glob
  9. import shutil
  10. import os
  11. import re
  12. import requests
  13. import urllib
  14. #路径变量
  15. SOURCEPATH=""
  16. Ops=""
  17. INSPECTION=""
  18. MONITOR=""
  19. SHELVES=""
  20. #地点变量
  21. add = "ADDRESS"
  22. #获取cookie
  23. FILENAME = open("cookie.txt", 'w+')
  24. filename = 'cookie.txt'
  25. ###
  26. #def GetCookie():
  27. # url = ''
  28. # file=urllib.request.urlopen(url)
  29. # #print(file.getcode)
  30. # message = file.info()
  31. # CookieStr = str(message)
  32. # CookieVlue = re.findall(r'Cacti=[a-zA-Z0-9]+',CookieStr)[0]
  33. # print(CookieVlue,file=FILENAME)
  34. # print(CookieVlue)
  35. # url=''
  36. # headers = {
  37. # 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
  38. # 'Cookie':CookieVlue,
  39. # 'Referer': '',
  40. # 'Host': '',
  41. # }
  42. #
  43. # First_Page = requests.get(url,headers=headers)
  44. # print (First_Page.text)
  45. #
  46. #GetCookie()
  47. ###
  48. def GetCookie():
  49. url = 'url'
  50. file=urllib.request.urlopen(url)
  51. #print(file.getcode)
  52. message = file.info()
  53. CookieStr = str(message)
  54. CookieVlue = re.findall(r'Cacti=[a-zA-Z0-9]+',CookieStr)[0]
  55. print(CookieVlue)
  56. return CookieVlue
  57. #爬取数据
  58. document = Document()
  59. url='url'
  60. headers = {
  61. 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
  62. 'Cookie':'Cacti=fdsafdsaw32342',
  63. }
  64. for i in (2,3,4,5,6,7,8,9,10,11,34,35,36):
  65. #目标页面
  66. for n in (1,3,6):
  67. #目标目录
  68. payload1 = {'action':'tree','tree_id':'2','leaf_id':'%s'%i,'page':'%d'%n}
  69. for m in(350,410,528,472,588,110,170,230,290,1085,1116,1142):
  70. #流量图ID号
  71. #正则规则
  72. Regular1 = '(graph\Simage\Sphp.*local_graph_id=%d.*end=\d+)'%m
  73. print (Regular1)
  74. First_Page = requests.get(url,headers=headers,params=payload1)
  75. print (First_Page.url)
  76. #清洗数据,获取流量图URL
  77. plt = re.findall(Regular1,First_Page.text)
  78. print(plt)
  79. if len(plt):
  80. a=(plt[0])
  81. else:
  82. True
  83. JPG_url = ( '<URL>'+ a)
  84. print( '<URL>'+ a)
  85. JPG_url_r = JPG_url.replace(';','&')
  86. print(JPG_url_r)
  87. #获取图片二进制数据,并保存成doc
  88. r = requests.get(JPG_url_r,headers=headers)
  89. image = Image.open(BytesIO(r.content))
  90. image.save('image%d.bmp'%i)
  91. document.add_picture('image%d.bmp'%i, width=Inches(6))
  92. document.save('FRALYCHEN.docx')
  93. #复制巡检报告文件并标记日期
  94. T=datetime.datetime.now()
  95. src_file = '00' + add + 'FRALYCHEN.xls'
  96. dst_file = "2019.{}.{}".format(T.month, T.day) + add + 'FRALYCHEN.xls'
  97. shutil.copyfile(SOURCEPATH + src_file,INSPECTION + dst_file)
  98. MonScfile = '00' + add + 'FRALYCHEN.docx'
  99. MonDtfile = "2019.{}.{}".format(T.month, T.day) + add + 'FRALYCHEN.docx'
  100. shutil.copyfile(SOURCEPATH + MonScfile,MONITOR + MonDtfile)
  101. #删除文件
  102. os.remove(SOURCEPATH + MonScfile)
  103. for infile in glob.glob(os.path.join(SOURCEPATH,'*.bmp')):
  104. os.remove(infile)
  105. #SVN提交
  106. def SvnCommit():
  107. os.chdir(Ops)
  108. os.system('svn cleanup')
  109. r=os.popen('svn st')
  110. info=r.readlines()
  111. print(info)
  112. for line in info:
  113. line=line.strip('\n').split(' ')
  114. one=line[0]
  115. two=line[1]
  116. print(two)
  117. if one == '?':
  118. os.system('svn add %s' % two)
  119. os.system('svn commit -m %s' % two)
  120. elif one == '!':
  121. os.system('svn del %s' %two)
  122. os.system('svn commit -m "clear"')
  123. SvnCommit()