下载给定网页上图片的方法

yipeiwu_com6年前Python基础
复制代码 代码如下:

# -*- coding: utf-8 -*-
import re
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 i in imglist:
urllib.urlretrieve(i,'%s.jpg' % x)
x+=1
html = getHtml(r'http://www.renren.com/')
getImg(html)

相关文章

Python跳出多重循环的方法示例

方法1:自定义异常 # -*- coding:utf-8 -*- """ 功能:python跳出循环 """ # 方法1:自定义异常 class Getoutofloop...

python 字典修改键(key)的几种方法

python 字典修改键(key)的几种方法

python中获取字典的key列表和value列表 # -*- coding: utf-8 -*- # 定义一个字典 dic = {'剧情': 11, '犯罪': 10, '动作...

python自动结束mysql慢查询会话的实例代码

生产环境的有些sql查询写得太复杂,或是表很大,对应索引未建立或建立不合理,或是查询未充分使用索引等,就有可能出现慢查询,一些慢查询需要修改程序,可能没那么快能解决,这时如果有个脚本能自...

python调用动态链接库的基本过程详解

python调用动态链接库的基本过程详解

动态链接库在Windows中为.dll文件,在linux中为.so文件。以linux平台为例说明python调用.so文件的使用方法。 本例中默认读者已经掌握动态链接库的生成方法,如果不...

python 显示数组全部元素的方法

如下所示: import numpy as np np.set_printoptions(threshold='nan') 以上这篇python 显示数组全部元素的方法就是小编分享...