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

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 简易计算器程序,代码就几行

代码: 复制代码 代码如下: import os while True: dynamic = input('输入计算表达式:') if dynamic != 'cls': try: re...

Python中几个比较常见的名词解释

循环(loop),指的是在满足条件的情况下,重复执行同一段代码。比如,while语句。 迭代(iterate),指的是按照某种顺序逐个访问列表中的每一项。比如,for语句。 递归(rec...

django 中QuerySet特性功能详解

Book表的数据显示 id title   price publish_id 2 Linux   30    1 3 项塔兰   45    2 4 追风筝的人...

pandas分别写入excel的不同sheet方法

pandas可以非常方便的写数据到excel,那么如何写多个dataframe到不同的sheet呢? 使用pandas.ExcelWriter import pandas as pd...

Python基础之函数的定义与使用示例

本文实例讲述了Python基础之函数的定义与使用。分享给大家供大家参考,具体如下: Python 定义函数使用 def 关键字,一般格式如下: def 函数名(参数列表):  ...