I'm trying to web scraping in python. I'm using python 3.6 and import necessary packages for this. But I encounter an Attribute Error of 'bytes' object has no attribute 'format'. If there is no format
method for bytes, how to do the formatting or "rewriting" of bytes
f = open(b'{0}.jpg'.format(flim.title.encode('utf8').replace(':','')) , 'wb')
f.write(requests.get(all_img[1]['src']).content)
f.close()
I was also facing the following problem
a bytes-like object is required, not 'str'
and get rid of it by using 'b' right before the format specifier.
UPDATE 1
I remove the 'b' prefix and also give 'w' mode only and now I get the following error: TypeError: a bytes-like object is required, not 'str'
Error
TypeError Traceback (most recent call last)
<ipython-input-7-7a0edc6fdfd0> in <module>()
----> 1 download_poster(get_list())
<ipython-input-6-49c729cbcc37> in download_poster(list_)
30 # f.write(requests.get(all_img[1]['src']).content)
31
---> 32 f = open('{0}.jpg'.format(flim.title.encode('utf-8').replace(':','')) , 'w')
33 f.write(requests.get(all_img[1]['src']).content)
34 f.close()
TypeError: a bytes-like object is required, not 'str'
I was wondering I might make a silly mistake. Excuse me for that. Thanks in advance.
UPDATE 2
I somehow able to fix this by changing some format. Using f-strings
(formatted string literals)
with open(f'{flim.title}.jpg', 'wb') as f:
f.write(requests.get(all_img[1]['src']).content)