python爬虫初学求帮看看

from re import findall
from urllib.request import urlopen
import os
import io
import gzip

url = 'https://blog.csdn.net/fanbest325/article/details/78913929'
image_path = 'D:\pachong'
os.chdir(image_path)

with urlopen(url) as fp:
    f = fp.read()
    print(f)
    a = gzip.decompress(fp)
    print(a)
    buff = io.BytesIO(f)
    m = gzip.GzipFile(fileobj=buff)
    f = m.read().decode('utf-8')
print(f)

pattern = 'img src="(.+?)"'
result = findall(pattern, f)
print(result)

for index, item in enumerate(result, 1):
    data = urlopen(str(item)).read()
    print('开始下载第' + str(index) + '张图片:' + str(item))
    f = open(str(index) + '.jpg', "wb")
    f.write(data)
    f.close()
请问这个为什么报错呀 显示的是TypeError: a bytes-like object is required, not 'HTTPResponse'
如果不加gzip解压又会显示未解压
#Python#
全部评论
a = gzip.decompress(fp) 这行写错了fp 改成f
点赞 回复
分享
发布于 2019-12-06 07:40

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务