1. 程式人生 > >python爬蟲:從頁面下載圖片以及編譯錯誤解決。

python爬蟲:從頁面下載圖片以及編譯錯誤解決。

#!/usr/bin/python
import re
import urllib


def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImage(html):
reg = r'src="(.*?\.jpg)" title'
image = re.compile(reg)
imglist = re.findall(image,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg' % x)
x+=1


html = getHtml("http://desk.zol.com.cn/tiyu/1920x1080/")
print(getImage(html))

報錯:

 “AttributeError: 'module' object has no attribute 'urlopen'”

原因是Python3裡的urllib模組已經發生改變,此處的urllib都應該改成urllib.request。

#!/usr/bin/python
import re
import urllib.request


def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
def getImage(html):
reg = r'src="(.*?\.jpg)" title'
image = re.compile(reg)
html = html.decode('GBK')
imglist = re.findall(image,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'%s.jpg' % x)
x+=1

html = getHtml("http://desk.zol.com.cn/tiyu/1920x1080/")
print(getImage(html))

發現讀取下來後,執行到第12行,出現:

can't use a string pattern on a bytes-like object

查找了一下,是說3.0現在的引數更改了,現在讀取的是bytes-like的,但引數要求是chart-like的,找了一下,加了個編碼:

html= html.decode('GBK')

#!/usr/bin/python
import re
import urllib.request

def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
def getImage(html):
reg = r'src="(.*?\.jpg)" title'
image = re.compile(reg)
html = html.decode('GBK')
imglist = re.findall(image,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'%s.jpg' % x)
x+=1


html = getHtml("http://desk.zol.com.cn/tiyu/1920x1080/")
print(getImage(html))



執行成功,從頁面下載圖片。