先上代码,后分析出现的问题:
1 #coding:utf-8
2 importre3 from bs4 importBeautifulSoup4 importgzip5 importurllib.request6 importurllib.parse7 importhttp.cookiejar8 importssl9 importtime10
11 defget_opener(heads):12 cj=http.cookiejar.CookieJar()13 pro=urllib.request.HTTPCookieProcessor(cj)14 opener=urllib.request.build_opener(pro)15 header=[]16 for key,value inheads.items():17 header.append((key,value))18 opener.addheaders=header19 returnopener20
21 defungzip(data):22 try:23 print("正在解压....")24 data=gzip.decompress(data)25 print("解压完成")26 except:27 print("无需解压")28 returndata29
30 if __name__=="__main__":31 ssl._create_default_https_context =ssl._create_unverified_context32 heads={33 "Accept":"text/html, application/xhtml+xml, */*",34 "Accept-Language":"zh-CN",35 "User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/2