python基础学习-7(简单爬虫)
1 |
实例:下载某URL下的jpg图片 |
01 |
#!/usr/bin/python |
02 |
import re #导入正则模块 |
03 |
import urllib #导入url模块 |
04 |
05 |
def getHtml(url): #获取url页面源代码 |
06 |
page = urllib.urlopen(url) |
07 |
html = page.read() |
08 |
return html |
09 |
10 |
def getImg(html): #下载url页面的jpg |
11 |
reg = r 'src="(.*?\.jpg)" .* width' #括号.*?里面为非贪婪匹配 |
12 |
imgre = re. compile (reg) #编译正则加快运行速度 |
13 |
imgList = re.findall(imgre,html) |
14 |
x = 1 |
15 |
for imgurl in imgList: |
16 |
urllib.urlretrieve(img, '%s.jpg' % x) #下载jpg,并命名 |
17 |
x = x + 1 |
18 |
url = raw_input ( "please input your download url:" ) #输入url |
19 |
html = getHtml(url) |
20 |
getImg(html) |