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

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打包方法Pyinstaller的使用

Python打包方法Pyinstaller的使用

Python是一个脚本语言,被解释器解释执行。它的发布方式: .py文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装Python并且安装依赖的各种库。(Py...

在Python中操作时间之strptime()方法的使用

 strptime()方法分析表示根据格式的时间字符串。返回值是一个struct_time所返回gmtime()或localtime()。 格式参数使用相同的指令使用strft...

scrapy自定义pipeline类实现将采集数据保存到mongodb的方法

本文实例讲述了scrapy自定义pipeline类实现将采集数据保存到mongodb的方法。分享给大家供大家参考。具体如下: # Standard Python library im...

pytorch中的上采样以及各种反操作,求逆操作详解

pytorch中的上采样以及各种反操作,求逆操作详解

import torch.nn.functional as F import torch.nn as nn F.upsample(input, size=None, scale_fact...

python简单鼠标自动点击某区域的实例

功能:间隔5毫秒,快速点击屏幕某区域,循环45000000次 from ctypes import * import time time.sleep(5) for i in rang...