The problem of utf-8' reporting an error I dont know what is wrong

generate an error such as" utf-8" codec can"t decode byte 0x8b in position 1: invalid start byte
my code:
from urllib import request
url = "http://odds.500.com/fenxi/yazhi-749525.shtml"
headers = {

"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"
}

req = request.Request (url)
resp = request.urlopen (req)
a = resp.read ()
print (a.decode ("utf-8"))-sharp-sharp-sharp problem description

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Apr.11,2021

The

page has been compressed by gzip and needs to be decompressed:

from urllib import request
import gzip

url = "http://odds.500.com/fenxi/yazhi-749525.shtml"
headers = {

"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"
}
req = request.Request(url)
resp = request.urlopen(req)
f = gzip.decompress(resp.read())
print(f.decode('gbk'))
< hr >

it is more recommended to directly use requests library:

import requests
url = "http://odds.500.com/fenxi/yazhi-749525.shtml"
content = requests.get(url).content

print(content.decode('gbk'))
Menu