一直挺喜欢逛虎扑的,今天想到下载虎扑的图片的问题,于是写了个爬虫,初级的规模,而且url也是写死的,玩玩也行嘛~~~
#!/usr/bin/python
#-*-coding:utf-8-*-
import re
import os
import urllib
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r'src="(.*?\.jpg)"'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
local = os.path.join('D:\Workspaces\Pythonworkspace\jpg','%s.jpg' % x) #提供路径
urllib.urlretrieve(imgurl,local)#使用urllib中的urlretrieve函数来下载图片
x+=1
html_1 = getHtml("http://bbs.hupu.com/9592923.html")#开始执行
getImg(html_1)
有图有真相: