国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

問題描述

這是報(bào)錯(cuò)信息:

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

改了好多地方,主要可能是目標(biāo)網(wǎng)站是gb2312編碼,這個(gè)程序在別的網(wǎng)站是可以正常下載圖片的,換上現(xiàn)在的網(wǎng)站就有問題還請各位多多指教,問題出在哪里?試了幾個(gè)方法都不行源碼如下:爬蟲圖片 - 請教各位: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://www.cgvv.com.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:

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

還有 urllib.request.urlretrieve(imgurl,local)這個(gè)imgurl不是一個(gè)合法的 url,只是一個(gè)相對 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://www.cgvv.com.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]) # 注意: 我這里沒有你那個(gè)錯(cuò)誤,我只需要改這個(gè)就行了 # 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')

標(biāo)簽: Windows系統(tǒng) win10
主站蜘蛛池模板: 加勒比一区在线 | 欧美孕妇性xxxⅹ精品hd | 精品久久久久久国产免费了 | 国产三级小视频 | 精品三级在线观看 | 日本特黄a级高清免费酷网 日本特黄特色 | 国产日韩精品一区在线观看播放 | 久久久久9999| 国产91在线 | 亚洲 | a毛片免费播放全部完整 | 三区在线视频 | 性色tv视频观看 | 亚洲伊人色一综合网 | 美女视频网站免费播放视 | 亚洲深夜视频 | 欧美日本高清视频在线观看 | 亚洲欧美一区二区三区国产精品 | 国产自在自线午夜精品 | 一个人看的免费观看日本视频www | 2017天天爽夜夜爽精品视频 | 亚洲欧洲一区二区三区久久 | 国产成人精品免费视频 | 久草视频福利 | 一及 片日本| 欧美一级看片a免费观看 | 欧美日本在线三级视频 | 性欧美一级毛片欧美片 | 精品久久久视频 | 精品久久久久久久久中文字幕 | 国产在线观看成人免费视频 | 亚洲另类激情综合偷自拍 | 国内精品免费一区二区观看 | 亚洲美女性生活视频 | jyzzjyzz国产免费观看 | 精品久久久中文字幕一区 | 国产一区二三区 | 欧美不卡在线视频 | 中文日韩字幕一区在线观看 | 131美女爱做免费毛片 | 国产高清视频免费在线观看 | 欧美大片在线播放 |