1062195630 发表于 2021-6-2 10:20:10

55节python

import urllib.request
response=urllib.request.urlopen('http://placekitten.com/g/500/600').read()
with open('mao.jpg','wb') as f:
    f.write(response)
response.geturl()
print(response.info())

Traceback (most recent call last):
File "C:/Users/liu/PycharmProjects/pythonProject1/venv/Scripts/55.py", line 3, in <module>
    with open('mao.jpg','wb',encoding='utf-8') as f:
ValueError: binary mode doesn't take an encoding argument

为什么会报错啊1

wp231957 发表于 2021-6-2 10:31:40

with open('mao.jpg','wb') as f:试一试 把那个b 去掉

Twilight6 发表于 2021-6-2 10:32:29


你代码直接 read() 获取网页源码了,返回 bytes 数据

你 geturl 只能对 urllib 获取的对象进行调用,但是却对 bytes 对象进行调用而导致报错

重新找个变量,赋值,对 urllib 对象进行调用函数即可,参考代码:

import urllib.request
response=urllib.request.urlopen('http://placekitten.com/g/500/600')
data = response.read()
with open('mao.jpg','wb') as f:
    f.write(data)
response.geturl()
print(response.info())
页: [1]
查看完整版本: 55节python