![](/img/trans.png)
[英]Error while trying to rename downloaded pdf file in Python Selenium
[英]Python: Hash of pdf file vs downloaded object
我想檢查網絡服務器上的pdf內容是否與我的計算機上的pdf內容相同。 我嘗試了一下但沒有成功:
>>> import requests, hashlib
>>> pdf = requests.get('<http link to pdf file>')
>>> type(pdf.content)
<class 'bytes'>
>>> type(repr(open('file.pdf','rb')).encode('utf-8'))
<class 'bytes'>
>>> hashlib.sha256(repr(open('file.pdf','rb')).encode('utf-8')) == hashlib.sha256(repr(pdf.content).encode('utf-8')).hexdigest()
False
>>> hashlib.sha256(repr(open('file.pdf','rb')).encode('utf-8')) == hashlib.sha256(pdf.content).hexdigest()
False
您正在散列文件對象的UTF-8編碼的repr
,而不是文件的內容。 無論如何,沒有理由使用repr
。 直接哈希內容。
>>> with open('file.pdf', 'rb') as f:
... h1 = hashlib.sha256(f.read()).digest()
>>> h2 = hashlib.sha256(pdf.content).digest()
>>> h1 == h2
True
第一個散列是文件對象表示形式的散列(而不是其內容):
repr(open('file.pdf','rb'))
# "<_io.BufferedReader name='file.pdf'>"
repr(open('file.pdf','rb')).encode('utf-8')
# b"<_io.BufferedReader name='file.pdf'>"
您的第一個哈希超過了bytes
: b"<_io.BufferedReader name='file.pdf'>"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.