import requests
import re
import hashlib
def md5_key(str):
m = hashlib.md5()
b = str.encode(encoding='utf-8')
m.update(b)
return m.hexdigest()
def get_id_tag(content, id_name):
id_name = id_name.strip()
patt_id_tag = """<[^>]*id=['"]?""" + id_name + """['" ][^>]*>"""
id_tag = re.findall(patt_id_tag, content, re.DOTALL|re.IGNORECASE)
if id_tag:
id_tag = id_tag[0]
return id_tag
def get_id_value(content, id_name):
content = get_id_tag(content, id_name)
id_name = id_name.strip()
patt_id_tag = """value=['](.*)[']"""
value = re.findall(patt_id_tag, content)
if value:
value = value[0]
return value
def getCountOfBugFromHtmlSource(content):
patt_count = "<strong>(.*)</strong> 条记录"
count = re.findall(patt_count, content)
if count:
count = count[0]
return count
def get_pwd(password, str1):
rand =get_id_value(str1, "verifyRand")
print(rand)
return md5_key(md5_key(password) + rand)
#登录的主方法
def login(baseurl_host,account,password, headers_base):
baseurl = baseurl_host + "/zentaopms/www/user-login.html"
#使用seesion登录,这样的好处是可以在接下来的访问中可以保留登录信息
session = requests.session()
#print(session.cookies)
#requests 的session登录,以post方式,参数分别为url、headers、data
content = session.get(baseurl,headers = headers_base)
#print(session.cookies)
#post需要的表单数据,类型为字典
login_data = {
'account': account,
'password': get_pwd(password, content.text),
'referer': 'http%3A%2F%2F192.168.100.98%3A8089%2Fzentaopms%2Fwww%2Fmy%2F',
}
content = session.post(baseurl, headers = headers_base,data = login_data)
print("--------登录详情-----------------")
print(content.text)
print("--------登录结束-----------------")
return session
print("-------------------------")
#再次使用session以get去访问网页,一定要带上heades
s = session.get("http://192.168.100.98:8089/zentaopms/www/my/", headers = headers_base)
print(s.text)
#把爬下来的首页写到文本中
f = open('chandaochandao.txt', 'w')
f.write(s.text)
def getCountOfBugCreateByName(url_host,session, headers_base):
url = url_host + "/zentaopms/www/search-buildQuery.html"
content = session.post(url, headers = headers_base,data = 'fieldtitle=&fieldkeywords=&fieldsteps=&fieldassignedTo=&fieldresolvedBy=&fieldstatus=&fieldconfirmed=ZERO&fieldproduct=4&fieldplan=&fieldmodule=0&fieldproject=&fieldseverity=0&fieldpri=0&fieldtype=&fieldos=&fieldbrowser=&fieldresolution=&fieldactivatedCount=&fieldtoTask=&fieldtoStory=&fieldopenedBy=&fieldclosedBy=&fieldlastEditedBy=&fieldmailto=&fieldopenedBuild=&fieldresolvedBuild=&fieldopenedDate=&fieldassignedDate=&fieldresolvedDate=&fieldclosedDate=&fieldlastEditedDate=&fielddeadline=&fieldid=&fieldbugfrom=&fieldbugproject=&fieldcustomercompany=&fieldgcprojectno=&fieldgcprojectmanager=&fieldtimecount=&fieldbugresource=&andOr1=AND&field1=openedBy&operator1=%3D&value1=yangjian&andOr2=and&field2=id&operator2=%3D&value2=&andOr3=and&field3=keywords&operator3=include&value3=&groupAndOr=and&andOr4=AND&field4=steps&operator4=include&value4=&andOr5=and&field5=assignedTo&operator5=%3D&value5=&andOr6=and&field6=resolvedBy&operator6=%3D&value6=&module=bug&actionURL=%2Fzentaopms%2Fwww%2Fbug-browse-4-0-bySearch-myQueryID.html&groupItems=3&queryID=&formType=lite')
print("**********************")
print(content.text)
url = url_host + "/zentaopms/www/bug-browse-4-0-bySearch-myQueryID.html"
content = session.get(url, headers = headers_base)
print(content.text)
count = getCountOfBugFromHtmlSource(content.text);
print(count)
return count
#程序从这里开始。
url_host = "http://192.168.100.98:8089"
account = "wuyongbo"
password = "test.1234"
headers_base = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'http://192.168.100.98:8089/zentaopms/www/user-login.html',
}
#进行登录,将账户信息替换成你的用户名和密码即可
session = login(url_host, account, password, headers_base)
count = getCountOfBugCreateByName(url_host, session, headers_base)
print("共 " + count + "条记录")
#print(md5_key(account))
#print(account + get_id_value("<input type='hidden' name='verifyRand' id='verifyRand' value='981871570' />", "verifyRand"))
#print(md5_key(md5_key(password) + "944069017"))
下面是绘制图形
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from datetime import datetime
import matplotlib.dates as mdates #处理日期
np.set_printoptions(suppress=True)
mpl.rcParams['font.sans-serif']=['SimHei'] #指定默认字体 SimHei为黑体
mpl.rcParams['axes.unicode_minus']=False #用来正常显示负
x = [20181210,
20181211,
20181212,
20181213,
20181214,
20181215,
20181217,
20181219,
20181220,
20181221,
20181224]
#总bug数
y = [362,
362,
364,
370,
370,
382,
382,
388,
390,
394,
400]
#解决了的bug数
y2 = [355,
355,
356,
359,
359,
369,
369,
375,
376,
379,
380]
xx = [str(d) for d in x]
xx = [datetime.strptime(d, '%Y%m%d').date() for d in xx]
plt.figure(figsize=(13,5))
plt.plot(xx,y,color='red',linewidth=1, marker='x',label = '累计bug数,单位(个)') #在当前绘图对象绘图(X轴,Y轴,蓝色虚线,线宽度)
plt.plot(xx,y2,color='green',linewidth=1, marker='x',label = '累计解决bug数,单位(个)') #在当前绘图对象绘图(X轴,Y轴,蓝色虚线,线宽度)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%d')) #设置x轴主刻度显示格式(日期)
plt.grid(True)
plt.legend(loc='lower right') #图例位置右下角
plt.xlabel('日期', color = 'r')
plt.ylabel('个数', color = 'r')
plt.axis('tight')
plt.title('A simple plot')
# 设置数字标签
for a, b in zip(xx, y):
plt.text(a, b, b, ha='center', va='bottom',color ='green', fontsize=12)
# 设置数字标签
for a, b in zip(xx, y2):
plt.text(a, b, b, ha='center', va='bottom',color ='blue', fontsize=12)
plt.show()