Python MD5散列相同的内容返回不同的散列
问题描述:
我正在写一个python程序,因为我懒惰,检查一个网站的工作开放我已经被告知并返回公司网页的所有工作。Python MD5散列相同的内容返回不同的散列
这里是我到目前为止的代码(是的,我知道的代码是jancky但我只是想获得它的工作)
import requests
from bs4 import BeautifulSoup
import sys
import os
import hashlib
reload(sys)
sys.setdefaultencoding('utf8')
res = requests.get('WEBSITE URL', verify=False)
res.raise_for_status()
filename = "JobWebsite.txt"
def StartUp():
if not os.path.isfile(filename):
try:
jobfile = open(filename, 'a')
jobfile = open(filename, 'r+')
print("[*] Succesfully Created output file")
return jobfile
except:
print("[*] Error creating output file!")
sys.exit(0)
else:
try:
jobfile = open(filename, 'r+')
print("[*] Succesfully Opened output file")
return jobfile
except:
print("[*] Error opening output file!")
sys.exit(0)
def AnyChange(htmlFile):
fileCont = htmlFile.read()
FileHash = hasher(fileCont, "File Code Hashed")
WebHash = hasher(res.text, "Webpage Code Hashed")
!!!!! Here is the Problem
print ("[*] File hash is " + str(FileHash))
print ("[*] Website hash is " + str(WebHash))
if FileHash == WebHash:
print ("[*] Jobs being read from file!")
num_of_jobs(fileCont)
else:
print("[*] Jobs being read from website!")
num_of_jobs(res.text)
deleteContent(htmlFile)
writeWebContent(htmlFile, res.text)
def hasher(content, message):
content = hashlib.md5(content.encode('utf-8'))
return content
def num_of_jobs(htmlFile):
content = BeautifulSoup(htmlFile, "html.parser")
elems = content.select('.search-result-inner')
print("[*] There are " + str(len(elems)) + " jobs available!")
def deleteContent(htmlFile):
print("[*] Deleting Contents of local file! ")
htmlFile.seek(0)
htmlFile.truncate()
def writeWebContent(htmlFile, content):
htmlFile = open(filename, 'r+')
print("[*] Writing Contents of website to file! ")
htmlFile.write(content.encode('utf-8'))
jobfile = StartUp()
AnyChange(jobfile)
我现在的问题是,我凑两个网站的HTML代码和文件的html代码。然而,这两个哈希值都不匹配,就像以前一样,我不确定,只能猜测它可能与保存在文件中的内容有关。哈希相隔不远太多,但它还是引起If语句每次
答
您所附加的显示两个散列的位置的截图对象fileHash
和webHash
失败。他们应该在不同的地点。
你真正想要比较的是两个散列对象的hexdigest()
。更改if
声明:
if FileHash.hexdigest() == WebHash.hexdigest():
print ("[*] Jobs being read from file!")
num_of_jobs(fileCont)
看看这个other StackOverflow answer一些更如何。
我明白你对会议地址的意思,在凌晨1点正在处理这个问题,只是飞过了我的头。然而,改变后哈希值仍然不同。我将更多地关注它,如果我修复它,我会报告回来。 –