【发布时间】:2018-11-26 19:37:41
【问题描述】:
我正在尝试找出一种方法来验证我的代码是否可以交叉验证索引文件中是否存在 url 字符串的 md5 转换值,如果可以则跳过扫描。
下面是我的代码
形成的 url 转换为 md5 字符串,然后在扫描完成后存储在 idx 文件中,目标是以后的扫描不应拾取相同的 url。我看到的问题是 if str(md5url) in line 没有被执行,可能是因为在将哈希添加到文件时没有使用 '\n' 作为后缀。但我试过了,还是不行。
有什么想法吗?
def computeMD5hash(string_for_hash):
m = hashlib.md5()
m.update(string_for_hash.encode('utf-8'))
return m.hexdigest()
def writefilehash(formation_URL):
fn="urlindex.idx"
try:
afile = open(fn, 'a')
afile.write(computeMD5hash(formation_URL))
afile.close()
except IOError:
print("Error writing to the index file")
fn="urlindex.idx"
try:
afile = open(fn, 'r')
except IOError:
afile = open(fn, 'w')
for f in files:
formation=repouri + "/" + f
#print(computeMD5hash(formation))
md5url=computeMD5hash(formation)
hashlist = afile.readlines()
for line in hashlist:
if str(md5url) in line:
print ("Skipping " + formation + " because its already scanned and indexed as " + line)
else:
if downloadengine(formation):
print ("Download completed " + formation)
print ("Starting to write to database..")
#writetodatabase()
print ("Writing hash value ..")
writefilehash(formation)
print("Closing..")
afile.close()
【问题讨论】:
-
您正在将一长行中的所有哈希值写入文件中。
md5url in line检查仍然会成功,但是换行在那里不起作用,in测试 containment,而不是相等性。