|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
那就改成 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()
|
|