亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

爬蟲圖片 - 請教各位:python爬蟲編碼問題,版本3.6,win10 64位下?

瀏覽:115日期:2022-07-14 15:35:50

問題描述

這是報錯信息:

Traceback (most recent call last): File 'D:pypic_downfrom2255ok.py', line 45, in <module> html = getHtml(url_all[i]) File 'D:pypic_downfrom2255ok.py', line 32, in getHtml html = response.read().decode()UnicodeDecodeError: ’utf-8’ codec can’t decode byte 0xb3 in position 184: invalid start byte

改了好多地方,主要可能是目標網站是gb2312編碼,這個程序在別的網站是可以正常下載圖片的,換上現在的網站就有問題還請各位多多指教,問題出在哪里?試了幾個方法都不行源碼如下:爬蟲圖片 - 請教各位:python爬蟲編碼問題,版本3.6,win10 64位下?

#coding=utf-8import urllib.requestfrom urllib.request import urlopen, urlretrieve import urllibimport urllib.parseimport reimport osfrom bs4 import BeautifulSoupurl_all =[’http://www.shop2255.com/showpro/2603.html’,’http://www.shop2255.com/showpro/1558.html’,’http://www.shop2255.com/showpro/1564.html’,’http://www.shop2255.com/showpro/2411.html’,’http://www.shop2255.com/showpro/2409.html’,’http://www.shop2255.com/showpro/1561.html’,’http://www.shop2255.com/showpro/2414.html’,’http://www.shop2255.com/showpro/2609.html’,’http://www.shop2255.com/showpro/2413.html’,’http://www.shop2255.com/showpro/2604.html’,’http://www.shop2255.com/showpro/2605.html’,’http://www.shop2255.com/showpro/2606.html’,’http://www.shop2255.com/showpro/2608.html’,’http://www.shop2255.com/showpro/2607.html’,’http://www.shop2255.com/showpro/2610.html’]def getHtml(url): response = urlopen(url) html = response.read().decode('gbk') return htmldef getImg(html): reg = ’src='http://m.lshqa.cn/wenda/(.+?.jpg)'’ imgre = re.compile(reg) imglist = re.findall(imgre,html) return imglistfor i in range(len(url_all)): html = getHtml(url_all[i]) list=getImg(html.decode()) x = 0 for imgurl in list:print(x)file_path = url_all[i](filepath,tempfilename) = os.path.split(file_path)(filename,extension) = os.path.splitext(tempfilename)if not os.path.exists(’d:%s’ % filename): os.mkdir(’d:%s’ % filename)# os.mkdir(’D:%s’ % filename2)local=r’D:%s%s.jpg’ % (filename,imgurl.splite('/')[-1])urllib.request.urlretrieve(imgurl,local)x+=1print('done')

問題解答

回答1:

# coding: utf-8import urllibimport requestsfrom pyquery import PyQuery as Qimport osbase_url = ’http://www.shop2255.com/’url_all =[’http://www.shop2255.com/showpro/2603.html’]for url in url_all: _, file_name = os.path.split(url) dir_name, _ = os.path.splitext(file_name) if not os.path.exists(dir_name):os.mkdir(dir_name) r = requests.get(url) for _ in Q(r.text).find(’img’):src = Q(_).attr(’src’)image_url = src if src.startswith(’http’) else os.path.join(base_url, src)_, image_name = os.path.split(image_url)image_path = os.path.join(dir_name, image_name)urllib.urlretrieve(image_url, image_path)回答2:

首先在你這個代碼里面 local=r’D:%s%s.jpg’ % (filename,imgurl.splite('/')[-1])中split寫成了splite.

還有 urllib.request.urlretrieve(imgurl,local)這個imgurl不是一個合法的 url,只是一個相對 url, 要改成絕對 url,需要加上 base_url = ’http://www.shop2255.com/’

還有生成的文件路徑好像也有問題.

# -*- coding: utf-8 -*-import urllib.requestfrom urllib.request import urlopen, urlretrieveimport urllibimport urllib.parseimport reimport osfrom bs4 import BeautifulSoupbase_url = ’http://www.shop2255.com/’url_all =[’http://www.shop2255.com/showpro/2603.html’,’http://www.shop2255.com/showpro/1558.html’,’http://www.shop2255.com/showpro/1564.html’,’http://www.shop2255.com/showpro/2411.html’,’http://www.shop2255.com/showpro/2409.html’,’http://www.shop2255.com/showpro/1561.html’,’http://www.shop2255.com/showpro/2414.html’,’http://www.shop2255.com/showpro/2609.html’,’http://www.shop2255.com/showpro/2413.html’,’http://www.shop2255.com/showpro/2604.html’,’http://www.shop2255.com/showpro/2605.html’,’http://www.shop2255.com/showpro/2606.html’,’http://www.shop2255.com/showpro/2608.html’,’http://www.shop2255.com/showpro/2607.html’,’http://www.shop2255.com/showpro/2610.html’]def getHtml(url): response = urlopen(url) # print(response.read()) html = response.read().decode('gbk') print(html) return htmldef getImg(html): reg = ’src='http://m.lshqa.cn/wenda/(.+?.jpg)'’ imgre = re.compile(reg) imglist = re.findall(imgre, html) return imglistfor i in range(len(url_all)): html = getHtml(url_all[i]) # 注意: 我這里沒有你那個錯誤,我只需要改這個就行了 # list = getImg(html.decode()) list = getImg(html) # print(list) x = 0 for imgurl in list:print(x)file_path = url_all[i](filepath, tempfilename) = os.path.split(file_path)(filename, extension) = os.path.splitext(tempfilename)if not os.path.exists(’d:%s’ % filename): os.mkdir(’d:%s’ % filename)# os.mkdir(’D:%s’ % filename2)local = r’D:%s%s.jpg’ % (filename, imgurl.split('/')[-1])try: urllib.request.urlretrieve(base_url + imgurl, local)except: print('can’t retrieve the' + base_url + imgurl)x += 1print('done')

標簽: Windows系統 win10
主站蜘蛛池模板: 亚洲精品色综合色在线观看 | 91精品国产手机 | 一级午夜a毛片免费视频 | 国产日韩欧美精品一区二区三区 | 欧美日韩国产在线观看一区二区三区 | 爽死你个放荡粗暴小淫货双女视频 | 国产孕妇孕交大片孕 | fc2在线播放 | 91亚洲免费| 思思久热re6这里有精品 | 99亚洲| 日韩中文字幕一在线 | 成人国产三级精品 | 一级国产精品一级国产精品片 | 欧美日本一区 | 欧美另类视频videosbest18 | 久久综合久美利坚合众国 | 欧美日韩在线视频一区 | 最新精品亚洲成a人在线观看 | 99精品免费久久久久久久久日本 | 欧美一区精品 | 国产性自爱拍偷在在线播放 | 日本三级2021最新理论在线观看 | 欧美亚洲另类久久综合 | 色老久久精品偷偷鲁一区 | 亚洲精品成人7777在线观看 | 九九免费在线视频 | 真实国产乱人伦在线视频播放 | 亚洲性生活视频 | 国产精品久久久久久久福利院 | 久久视频精品36线视频在线观看 | 爱久久精品国产 | 久久精品亚洲精品国产欧美 | 国产一级在线观看视频 | 亚洲午夜精品 | 日本天堂网在线 | 国产资源精品一区二区免费 | 一区二区网站在线观看 | 欧美成人观看免费版 | 欧美做爰野外在线视频观看 | 日本免费的一级绿象 |