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))



运行成功,从页面下载图片。

你可能感兴趣的:(Python)