‘latin-1‘ codec can‘t encode characters in position 0-10: ordinal not in ran

python爬虫
pycharm
今天在照着教程学的时候出现错误

from lxml import etree
import time
import requests
import time
from random import randint
from bs4 import BeautifulSoup


import unicodedata
import re

def sleep():
    i = randint(1,3)
    time.sleep(i)

if __name__ == "__main__":
    url = 'http://pic.netbian.com/4kyouxi/'
    headers = {
        'User-Agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64) AppleWebKit/537.36(KHTML, likeGecko) Chrome / 83.0.4103.116Safari / 537.36'
    }
    res = requests.get(url=url,headers=headers)
    # res.encoding='utf-8' # img_name 中文   可能成功/不成功  与后者二选一
    page_text = res.text
    #
    tree = etree.HTML(page_text)
    li_list = tree.xpath('//div[@class="slist"]/ul/li')
    for li in li_list:

        img_src = 'http://pic.netbian.com'+li.xpath('./a/img/@src')[0]


        img_name = li.xpath('./a/img/@alt')[0] +'.jpg'
        # 较为通用的解决中文乱码问题
        img_name = img_name.encode('iso-8859-1').decode('gbk')

        print(img_name,img_src)

前面写了res.encoding='utf-8'后面再写img_name = img_name.encode('iso-8859-1').decode('gbk')就报错了,删掉前面的,成功运行

《最后生还者2 The Last of Us: Part 2》4k游戏壁纸3840x2160.jpg http://pic.netbian.com/uploads/allimg/200730/000118-15960384780681.jpg
lol英雄联盟Spirit Blossom 4k高清壁纸3840x2160.jpg http://pic.netbian.com/uploads/allimg/200731/235753-15962110737fa8.jpg
《绝地求生 全军出击》4K游戏壁纸.jpg http://pic.netbian.com/uploads/allimg/180926/160441-1537949081adfd.jpg
尼尔机械纪元4k壁纸3840x2160.jpg http://pic.netbian.com/uploads/allimg/170404/171148-14912971083455.jpg
《League of Angels 2》女神联盟2命运女神4k壁纸.jpg http://pic.netbian.com/uploads/allimg/190426/222643-15562888034954.jpg
绝地求生 吃鸡Pubg头盔女孩4k游戏壁纸.jpg http://pic.netbian.com/uploads/allimg/200801/001813-15962122934e5e.jpg
绝地求生Pubg 吃鸡4k原画高清电脑壁纸3840x2160.jpg http://pic.netbian.com/uploads/allimg/200801/001611-1596212171869b.jpg
《陷落封印:仲裁者之印 Fell Seal_ Arbiters Mark - Missions and Monsters 》4k壁纸.jpg http://pic.netbian.com/uploads/allimg/200728/215822-1595944702a4ca.jpg
刺客信条英灵殿 人物原画4k游戏壁纸3840x2160.jpg http://pic.netbian.com/uploads/allimg/200728/215409-1595944449f37a.jpg
《刺客信条英灵殿 Assassins Creed_ Valhalla》3440x1440带鱼屏壁纸.jpg http://pic.netbian.com/uploads/allimg/200727/190821-1595848101b127.jpg
《刺客信条 英灵殿 Assassins Creed_ Valhalla》游戏人物大全4k壁纸.jpg http://pic.netbian.com/uploads/allimg/200727/185658-159584741855a0.jpg
lol九尾妖狐 灵魂莲华 阿狸4k壁纸.jpg http://pic.netbian.com/uploads/allimg/200722/213243-15954247639a7f.jpg
lol英雄联盟魔蛇之拥 灵魂莲华 卡西奥佩娅4k壁纸.jpg http://pic.netbian.com/uploads/allimg/200722/212535-1595424335547c.jpg
虎魄张飞王者荣耀4k壁纸3840x2160.jpg http://pic.netbian.com/uploads/allimg/200723/171846-15954959265ce6.jpg
lol英雄联盟放逐之刃 灵魂莲华 锐雯4k电脑壁纸.jpg http://pic.netbian.com/uploads/allimg/200722/213349-1595424829e179.jpg
英雄联盟lol永猎双子 灵魂莲华 千珏4k游戏壁纸.jpg http://pic.netbian.com/uploads/allimg/200722/212743-1595424463afbe.jpg
一念神魔李信王者荣耀4k高清壁纸3840x2160.jpg http://pic.netbian.com/uploads/allimg/200714/155458-1594713298c491.jpg
2020绝地求生吃鸡最火4k壁纸.jpg http://pic.netbian.com/uploads/allimg/200714/231924-1594739964870f.jpg
王者荣耀一念神魔 李信3440x1440带鱼屏游戏壁纸.jpg http://pic.netbian.com/uploads/allimg/200714/155229-1594713149f53e.jpg
王者荣耀一念神魔 李信5120x2160游戏壁纸.jpg http://pic.netbian.com/uploads/allimg/200714/155133-1594713093d0a5.jpg
王者荣耀一念神魔 李信5120x1440双屏游戏壁纸.jpg http://pic.netbian.com/uploads/allimg/200714/155011-15947130117266.jpg

Process finished with exit code 0

你可能感兴趣的:(python报错,python)