鱼C论坛

 找回密码
 立即注册
查看: 1191|回复: 2

[已解决]UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 65: inva...

[复制链接]
发表于 2022-3-17 20:43:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
萌新学习爬取网站图片,但网站编码不了‘utf-8’,怎么办,请求各位大佬指点迷津!

import urllib.request
import os

def url_open(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.69')
    response = urllib.request.urlopen(url)
    html = response.read()
    return html
   
def find_imgs(url):
    print(url)
    html = url_open(url).decode("utf-8")  #报错的位置
    print(html)
   
    img_addrs = []

    a = html.find('img alt')
    b = html.find('src=',a)
    c = html.find('.jpg',b,b+255)

    if c !=-1:
        img_addrs.append(html[b+5:c+4])

    for each in img_addrs:
        print(each)

def save_imgs(folder,img_addrs):
    pass

def download_photo(folder = 'photo',pages = 10):
    try:
        os.mkdir(folder)
    except FileExistsError:
         pass
    os.chdir(folder)

    url = "http://www.daimg.com/photo/"
    page_num = int(220622)

    for i in range(10):
        page_num += i
        page_url = url + '202202/photo_' + str(page_num) + '.html'
        img_addrs = find_imgs(page_url)
        save_imgs(folder,img_addrs)

if __name__=='__main__':
    download_photo()

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
============== RESTART: D:\python-3.6.5-amd64\download_photo.py ==============
http://www.daimg.com/photo/202202/photo_220622.html
Traceback (most recent call last):
  File "D:\python-3.6.5-amd64\download_photo.py", line 48, in <module>
    download_photo()
  File "D:\python-3.6.5-amd64\download_photo.py", line 44, in download_photo
    img_addrs = find_imgs(page_url)
  File "D:\python-3.6.5-amd64\download_photo.py", line 13, in find_imgs
    html = url_open(url).decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 65: invalid start byte
最佳答案
2022-3-18 13:47:49
那就改成 gbk 呗
import urllib.request
from urllib.request import HTTPError
import os

def url_open(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.69')
    response = urllib.request.urlopen(url)
    html = response.read()
    return html
   
def find_imgs(url):
    print(url)
    html = url_open(url).decode("gbk")  #报错的位置
    print(html)
   
    img_addrs = []

    a = html.find('img alt')
    b = html.find('src=',a)
    c = html.find('.jpg',b,b+255)

    if c !=-1:
        img_addrs.append(html[b+5:c+4])

    for each in img_addrs:
        print(each)

def save_imgs(folder,img_addrs):
    pass

def download_photo(folder = 'photo',pages = 10):
    try:
        os.mkdir(folder)
    except FileExistsError:
         pass
    os.chdir(folder)

    url = "http://www.daimg.com/photo/"
    page_num = int(220622)

    for i in range(10):
        page_num += i
        page_url = url + '202202/photo_' + str(page_num) + '.html'
        try:
            img_addrs = find_imgs(page_url)
        except HTTPError:
            continue
        save_imgs(folder,img_addrs)

if __name__=='__main__':
    download_photo()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-18 13:47:49 | 显示全部楼层    本楼为最佳答案   
那就改成 gbk 呗
import urllib.request
from urllib.request import HTTPError
import os

def url_open(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.69')
    response = urllib.request.urlopen(url)
    html = response.read()
    return html
   
def find_imgs(url):
    print(url)
    html = url_open(url).decode("gbk")  #报错的位置
    print(html)
   
    img_addrs = []

    a = html.find('img alt')
    b = html.find('src=',a)
    c = html.find('.jpg',b,b+255)

    if c !=-1:
        img_addrs.append(html[b+5:c+4])

    for each in img_addrs:
        print(each)

def save_imgs(folder,img_addrs):
    pass

def download_photo(folder = 'photo',pages = 10):
    try:
        os.mkdir(folder)
    except FileExistsError:
         pass
    os.chdir(folder)

    url = "http://www.daimg.com/photo/"
    page_num = int(220622)

    for i in range(10):
        page_num += i
        page_url = url + '202202/photo_' + str(page_num) + '.html'
        try:
            img_addrs = find_imgs(page_url)
        except HTTPError:
            continue
        save_imgs(folder,img_addrs)

if __name__=='__main__':
    download_photo()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-19 16:46:53 | 显示全部楼层
isdkz 发表于 2022-3-18 13:47
那就改成 gbk 呗

谢谢大佬,问题解决了,等学完了基本语法,就去了解一下编码,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-12 01:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表