代码: 全选
# coding=utf-8
import re
import urllib.request
import os
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read().decode('utf-8')
return html
def getImg(html):
reg = r'src="(.+?\.jpg)"'
imgre = re.compile(reg)
imglist = imgre.findall(str(html))
print(imglist)
x = 0
try:
os.mkdir("pic")
except :
pass
for imgurl in imglist:
try:
conn = urllib.request.urlopen(imgurl)
f = open("pic\{0}.jpg".format(x),'wb')
f.write(conn.read())
f.close()
x=x+1
except :
pass
html = getHtml("http://www.gamersky.com/")
getImg(html)