|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import urllib.request
- import os
- def url_open(url):
- res = urllib.request.Request(url)
- res.add_header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36")
- response = urllib.request.urlopen(res)
- html = response.read()
- return html
- def find_img(url):
- html = url_open(url).decode("utf-8")
- a = html.find("img src=")
- img_addrs=[]
- while a!=-1:
- b = html.find('id="bigpicimg"',a,a+255)
- if b==-1:
- img_addrs.append(html[a+9:b-2])
- else :
- b=a
- a = html.find("img src=",b)
-
- for i in img_addrs:
- print(i)
-
- def download(folder = "chengduling",page = 5):
- os.mkdir(folder)
- os.chdir(folder)
- url="http://www.tupianzj.com/mingxing/xiezhen/20180625/"
- for i in range(1,6):
- if i==1:
- page_num = "163771"
- else:
- page_num = "163771"+"_"+str(i)
- page_url = url + page_num + ".html"
- img_url = find_img(page_url)
- if __name__ == "__main__":
- download()
复制代码
- Traceback (most recent call last):
- File "C:/Users/123/Desktop/chengduling.py", line 43, in <module>
- download()
- File "C:/Users123/Desktop/chengduling.py", line 39, in download
- img_url = find_img(page_url)
- File "C:/Users/123/Desktop/chengduling.py", line 12, in find_img
- html = url_open(url).decode("utf-8")
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 309: invalid start byte
复制代码
编码报错要怎么解决
|
|