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=['](.*)[']"""
id_tag = re.findall(patt_id_tag, content)
if id_tag:
id_tag = id_tag[0]
return id_tag
def getCountOfBugFromHtmlSource(content):
patt_count = "(.*) 条记录"
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(login_data)
print("-------------------------")
print("-------------------------")
#再次使用session以get去访问网页,一定要带上heades
s = session.get("http://192.168.100.98:8089/zentaopms/www/my/", headers = headers_base)
print(s.text)
print(s.text.encode('utf-8'))
#把爬下来的首页写到文本中
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("", "verifyRand"))
#print(md5_key(md5_key(password) + "944069017"))