Wrote a hash report wrong. Why?

Traceback (most recent call last):
  File "/home/shenjianlin/.local/lib/python3.4/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/shenjianlin/my_project/Espider/Espider/pipelines/searchwebsitepipeline.py", line 37, in process_item
    iconUrl=self.get_max_size_url(item["iconUrl"])
  File "/home/shenjianlin/my_project/Espider/Espider/pipelines/searchwebsitepipeline.py", line 18, in get_max_size_url
    save_path = os.path.join("./image", hashlib.sha1(res.content))
  File "/usr/lib64/python3.4/posixpath.py", line 79, in join
    if b.startswith(sep):
AttributeError: "_hashlib.HASH" object has no attribute "startswith"-sharp-sharp-sharp 


Dec.16,2021

type is incorrect.
os.path.join expects to pass a parameter of type string, while you pass in an object of type _ hashlib.HASH .

>>> import hashlib
>>> hashlib.sha1(b'this is plaintext')
<sha1 HASH object @ 0x7f0a100539e0>

I guess you want to get the hexadecimal result of HASH . You can use the hexdigest method, like this:

>>> print(hashlib.sha1(b'this is plaintext').hexdigest())
29487b3263304dba8c04fbe1169a8b8044e0bf8e

or return Bytes type, and then transcode (not recommended):

>>> print(hashlib.sha1(b'this is plaintext').digest())
b')H{2c0M\xba\x8c\x04\xfb\xe1\x16\x9a\x8b\x80D\xe0\xbf\x8e'

reference:
python.org/3.4/library/hashlib.html" rel=" nofollow noreferrer "> python hashlib doc


Python module analysis: section 2-hashlib encryption module

Menu