实习 week(二)
上一次任务:
cms版本识别提取模块
从phpmyadmin的若干版本源码包中,找出一个路径(包括文件名),该路径大多数源码包中都包含,并且md5值不一样,寻找的文件类型为css或js
程序输出:
{
"url": "/test/test.css",
"data": {
"xxxversion": "32位md5",
"yyyversion": "32位md5"
}
}
phpmyadmin查看了一下,共更新了几百个版本。
手动下载不现实,在Linux系统中使用wget -i filename.txt 可实现。
那么就需要将所有版本的下载链接保存到filename.txt文件中。
1、使用爬虫进行链接的获取
由于phpmyadmin软件源代码在每个网页中只有10个版本需要点击Next跳转到下一页,那么就需要找到该网页中的下一页的链接,即递归爬取。可以使用Beautiful Soup, 是一个处理Python HTML/XML的模块,功能相当强劲。
Beautiful Soup是一个库,最主要的功能是从网页抓取数据。
下载及安装:https://blog.csdn.net/a_flying_bird/article/details/51088485
代码实现:
import re
from urllib.request import urlopen, urlretrieve
from bs4 import BeautifulSoup
zip_net_list=[]# 下载HTML
def getHtml(url):page = urlopen(url)html = page.read()return html# 从html中解析URL
def get(html):reg = r'href="(.*?\.zip)"'netre = re.compile(reg)htmld = html.decode('utf-8')netlist = netre.findall(htmld)return netlistdef find_file(url):# url = 'https://github.com/phpmyadmin/phpmyadmin/tags?after=RELEASE_4_6_6'html = getHtml(url)netlist = get(html)for neturl in netlist:neturl1,neturl2=neturl.strip().split('/a')neturl3,neturl4=neturl2.strip().split('/',1)neturl5,neturl6=neturl4.strip().split('.z')print('https://codeload.github.com'+neturl1+'/'+'zip/'+neturl5)zip_net_list.append('https://codeload.github.com'+neturl1+'/'+'zip/'+neturl5)return Falsedef crawl(url):html = urlopen(url)# print('--------------------------------')find_file(url)html = urlopen(url)soup = BeautifulSoup(html, 'html.parser')zzr = soup.find_all('div', class_="pagination")list_net = []for item in zzr:list_tmp = item.find_all('a')for a in list_tmp:list_net.append(a.get('href'))# print(list_net)len_ = len(list)if len(list_net)==1:if len_>2 and list_net[0]==list[len_-2]:return listlist.append(list_net[0])crawl(list_net[0])if len(list_net)==2:# else:list.append(list_net[1])crawl(list_net[1])url='https://github.com/phpmyadmin/phpmyadmin/tags'
list=[]
list.append(url)
crawl(url)save_zip_net=open('phpmyadmin_net.txt','w')
for i in range(len(zip_net_list)):save_zip_net.write('%s\n'%zip_net_list[i])
2、批量下载
wget -i phpmyadmin_net.txt
3、批量解压到相应的目录
ls * | xargs -n1 unzip -d 解压目录
到解压目录查看当前文件夹数量:
ls |wc -l
共有446个版本
4、计算MD5
find ./ -regextype posix-extended -regex ".*\.(css|js)" -exec md5sum {} \;>phpmyadmin_md5.txt
5、理解
任务要求:找出一个路径(包括文件名),该路径大多数源码包中都包含
上次理解错误以为要求所有版本中都含有相同路径的文件(即446个版本中都有相同路径的相同文件),其实在代码中加一个版本数量的遍历,起初是446表示446都含有相同路径的文件,如果没有,就减1,找445个版本中有相同路径的文件。。。,直到X个版本中找到了为止。接下来就要考虑该X版本中有多少个数量为X相同路径的文件,取出其中某个相同路径的MD5不同的数量最多的那个,可能不止一个。
6、代码整理
#对数据分割重组
import re
import os
import sys
#--------------创建all_file_merge.txt----V/P:MD5-----------
#所有版本内容合并: V/P:md5
all_file_to_list=[]######*********######### 可修改
file_num_ = 446
# file_num = 15
filepath='phpmyadmin_md5.txt' ######*********######### 可修改
# filepath='new_file.txt'
file=open(filepath,'r')
count__=0
# print('初始长度:\n',len(file.readlines()))
for line in file.readlines():key,value=line.strip().split('./')all_file_to_list.append(value+':'+key) #value:versionpath key:md5count__+=1# print(count__)
# print(all_file_to_list) #all_file_merge.txt#------------创建path_md5.txt---P:MD5------------
list_path_md5_list=[]for li in all_file_to_list:value, key=li.strip().split(':')value=value[::-1]match=re.match('.*/',value)value=match.group()[::-1]# dict[value]=keylist_path_md5_list.append(value+':'+key)# print(list_path_md5_list) #path_md5.txt
#
# print('初始列表总长度:\n',len(list_path_md5_list))#--------------创建version_md5.txt-V:MD5---------------list_version_md5=[]for li in all_file_to_list:(key, value)=li.strip().split(':')version,path=key.strip().split('/',1)list_version_md5.append(version+':'+value)
# print(list_version_md5)
# print('success!')#--------------------------------第2步:#文件排序
# file=open('txt2/path_md5.txt','r') ######*********#########
#对path_md5排序:结果写入path_md5_sort.txt文件中list_path_md5=[]
# for i in range(len(list_path_md5_dict)):
# for key,value in list_path_md5_dict[i].items():
# list_path_md5.append(key+':'+value)for i in range(len(list_path_md5_list)):list_path_md5.append(list_path_md5_list[i])# for line in file.readlines():
# print(len(list))list_path_md5.sort()#返回值是None,其表示是在原对象上进行的操作。返回排序后结果意味着创建了一个副本。list_path_md5_sort=list_path_md5# print(len(list_path_md5_sort))
# print('路径排序:\n',list_path_md5_sort)#----------------------------------第三步:#小心:在同一个文件中不能写读同一个文件,否则读会出错
#找到所有版本中共同的文件 路径相同list_path=[]
for i in range(len(list_path_md5_sort)):parh,md5=list_path_md5_sort[i].strip().split(':')list_path.append(parh)# print('路径总数:\n',len(list_path))#
# print('路径列表:\n',list_path)# save_list_path=open('save_same_list_path.txt','w')
# for li in list_path:
# save_list_path.write('%s\n'%li)#记录列表中相同路径的第一个序号:与源文件少1,是因为列表从0开始遍历而源文件是从1开始计数
list_path_record_num=[]
file_num=0
for num_p in range(446,0,-1):# print(len(path_list))for i in range(len(list_path)):# print(i)count = i + num_p - 1 # [i count]范围判断if count=list_path_record_num[num] and \i<=list_path_record_num[num]+file_num-1:##############list_all_samefile_path.append(j)count+=1# print(line)if(count==file_num):#计数到16时,count归0,并且列表指向下一个元素#########修改num+=1count=0i+=1# print('获得文件数量:',len(list_path_record_num))
# print('存储的序号:\n',list_path_record_num)
#
# print('列表元素个数:',len(list_all_samefile_path))
#
#
# print('相同路径下的MD5:\n',list_all_samefile_path)
#
#
#
#-----------第5步从相同路径下的文件中找到不同的MD5list_samepath_different_md5=[]
num=0
for i in range(len(list_all_samefile_path)):# for j in range(16)if i+1
修改code:
#对数据分割重组
import re
import os
import sys
#--------------创建all_file_merge.txt----V/P:MD5-----------
#所有版本内容合并: V/P:md5
all_file_to_list=[]######*********######### 可修改
file_num_ = 446filepath='phpmyadmin_md5.txt' ######*********######### 可修改file=open(filepath,'r')
count__=0for line in file.readlines():key,value=line.strip().split('./')all_file_to_list.append(value+':'+key) #value:versionpath key:md5count__+=1#------------创建path_md5.txt---P:MD5------------
list_path_md5_list=[]for li in all_file_to_list:value, key=li.strip().split(':')value=value[::-1]match=re.match('.*/',value)value=match.group()[::-1]# dict[value]=keylist_path_md5_list.append(value+':'+key)#--------------创建version_md5.txt-V:MD5---------------list_version_md5=[]for li in all_file_to_list:(key, value)=li.strip().split(':')version,path=key.strip().split('/',1)list_version_md5.append(version+':'+value)#--------------------------------第2步:list_path_md5=[]for i in range(len(list_path_md5_list)):list_path_md5.append(list_path_md5_list[i])list_path_md5.sort()#返回值是None,其表示是在原对象上进行的操作。返回排序后结果意味着创建了一个副本。list_path_md5_sort=list_path_md5#----------------------------------第三步:list_path=[]
for i in range(len(list_path_md5_sort)):parh,md5=list_path_md5_sort[i].strip().split(':')list_path.append(parh)#记录列表中相同路径的第一个序号:与源文件少1,是因为列表从0开始遍历而源文件是从1开始计数
list_path_record_num=[]
file_num=0
for num_p in range(446,0,-1):# print(len(path_list))for i in range(len(list_path)):# print(i)count = i + num_p - 1 # [i count]范围判断if count=list_path_record_num[num] and \i<=list_path_record_num[num]+file_num-1:##############list_all_samefile_path.append(j)count+=1# print(line)if(count==file_num):#计数到16时,count归0,并且列表指向下一个元素#########修改num+=1count=0i+=1#-----------第5步从相同路径下的文件中找到不同的MD5list_samepath_different_md5=[]
num=0
for i in range(len(list_all_samefile_path)):# for j in range(16)if i+1
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
