我正在尝试在python中进行网页抓取。我正在使用python 3.6并为此导入必要的包。但我遇到属性错误'bytes'对象没有属性'format'。如果字节没有format
方法,那么如何格式化或“重写”字节
f = open(b'{0}.jpg'.format(flim.title.encode('utf8').replace(':','')) , 'wb')
f.write(requests.get(all_img[1]['src']).content)
f.close()
我也遇到了以下问题
a bytes-like object is required, not 'str'
并在格式说明符之前使用'b'去除它。
更新1
我删除'b'前缀并且只给'w'模式,现在我得到以下错误: TypeError:需要类似字节的对象,而不是'str'
错误
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'
我想知道我可能犯了一个愚蠢的错误。对不起,请原谅。提前谢谢。
更新2
我能通过改变某种格式来解决这个问题。使用f-strings
(格式化的字符串文字)
with open(f'{flim.title}.jpg', 'wb') as f:
f.write(requests.get(all_img[1]['src']).content)
答案 0 :(得分:2)
这个问题非常简单。
Python支持'someString'
r'something'
:这是一个原始字符串,通常在您使用文件路径时使用,例如file_path = r'C:\MyProjects\Python\TestProject'. It tells the interpreter that this is a
原始字符串and no escape sequences (e.g.
\ n`)在这里有意义。f'something'
:这是[{1}}中提供的string interpolation
功能,可让您在字符串中设置占位符来设置值。例如,python3.5+
my_variable = 5
some_string = f'I have {my_variable} apples with me'
# prints I have 5 apples with me
:这表示b'something'
虽然您在此输入字符串,但由于字符串是bytes
的序列,解释器会将前缀bytes
理解为字节。 因为你有b
python将其理解为b'{0}.jpg'
而不是字符串。 bytes
是字符串函数,而不是.format()
函数,因此错误
bytes
如何解决?
很简单,您只需从Attribute Error: 'bytes' object has no attribute 'format'
删除前缀b
即可开始工作。
您提到的第二个问题:
现在关于第二个问题,您已经以b'{0}.jpg'
模式打开了文件,该模式代表wb
,因为它期望write bytes
作为输入进行写入。
如何解决:
只需以bytes
模式打开文件即可接受w
。我马上就会添加一个样本。
您的更新代码:
strings
或者,如果您使用的是f = open('{0}.jpg'.format(flim.title.replace(':','')) , 'w')
f.write(requests.get(all_img[1]['src']).content)
f.close()
,我会使用字符串插值:
python3.5+
此外,更好的方法是使用f = open(f'{flim.title.replace(':','')}.jpg', 'w')
f.write(requests.get(all_img[1]['src']).content)
f.close()
关键字来避免任何with
Resource Leaks
您不需要执行with open(f'{flim.title.replace(':','')}.jpg', 'w') as f:
f.write(requests.get(all_img[1]['src']).content)
,因为f.close()
语句会自动为您关闭文件。
答案 1 :(得分:1)
您已在wb
模式下打开文件,因此文件要求byte
不是字符串。
您可以执行以下操作之一
以w
模式打开文件。
将数据转换为字节。
以w
模式打开文件:
Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('my.txt', 'wb') as f:
... f.write('123')
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> with open('my.txt', 'w') as f:
... f.write('123')
...
3
>>>
将数据转换为字节:
Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('my.txt', 'wb') as f:
... f.write('123')
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> with open('my.txt', 'wb') as f:
... f.write(b'123')
...
3
>>>