类型错误 - 不需要 'str' 类字节对象 - Python 3.6

Type Error - not 'str' bytes-like object is required - Python 3.6

我正在尝试在 python 中进行网页抓取。我正在使用 python 3.6 并为此导入必要的包。但是我遇到了 'bytes' object has no attribute 'format' 的属性错误。如果bytes没有format方法,bytes

的格式化或者"rewriting"怎么办
 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)

您已在 wb 模式下打开文件,因此文件需要 byte 而不是字符串。

您可以执行以下操作之一,

  1. w 模式打开文件。

  2. 将数据转换为字节。

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
>>>

这个问题很微不足道。

前言

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' :这是 python3.5+ 中可用的 string interpolation 功能,允许您在字符串中使用占位符来设置值。例如,

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 理解为字节。

您的问题:

因为你有一个 b'{0}.jpg' python 将它理解为 bytes 而不是字符串。 .format() 是一个字符串函数而不是 bytes 函数,因此错误

Attribute Error: 'bytes' object has no attribute 'format'

如何解决?

很简单,您只需从 b'{0}.jpg' 中删除前缀 b 即可开始工作。

您提到的第二个问题: 现在关于第二个问题,您已经以 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(),因为 with 语句会自动为您关闭文件。