Python使用正则匹配实现抓图代码分享

yipeiwu_com6年前Python基础

内涵:正则匹配,正则替换,页面抓取,图片保存 。

实用的第一次 Python 代码 参考

#!/usr/bin/env python
import urllib
import re
 
x=0
def getHtml(url):
 page = urllib.urlopen(url)
 html = page.read()
 return html
 
def getImg(html):
 global x
 reg = 'alt=".+?" src="(.+?\.jpg)"'
 imgre = re.compile(reg)
 imglist = re.findall(imgre,html)
 for imgurl in imglist:
  urllib.urlretrieve(re.sub(r',\d+,\d+',',800,450',imgurl),"img/%s.jpg" % x)
  print "\n"+re.sub(r',\d+,\d+',',800,450',imgurl)+"========"+"img/%s.jpg" % x
  x+=1
 
print 'Starting...'
 
pages = range(1,9)
 
for p in pages:
 html = getHtml('http://m.lovebizhi.com/category/7655/%d/' % p)
 print "\n-------------------------page:%d-------------------------------" % p
 getImg(html)
 
print "\nDone!"

以上所述就是本文给大家分享的全部代码了,本人Python菜鸟,第一个作品,希望对大家能有所帮助。

相关文章

python连接MySQL数据库实例分析

python连接MySQL数据库实例分析

本文实例讲述了python连接MySQL数据库的方法。分享给大家供大家参考。具体实现方法如下: import MySQLdb conn = MySQLdb.connect(host=...

实例探究Python以并发方式编写高性能端口扫描器的方法

关于端口扫描器 端口扫描工具(Port Scanner)指用于探测服务器或主机开放端口情况的工具。常被计算机管理员用于确认安全策略,同时被攻击者用于识别目标主机上的可运作的网络服务。 端...

Python操作redis实例小结【String、Hash、List、Set等】

本文实例总结了Python操作redis方法。分享给大家供大家参考,具体如下: python连接方式可参考:/post/161353.htm 这里介绍详细使用 1、String 操作 r...

Python求均值,方差,标准差的实例

如下所示: import numpy as np arr = [1,2,3,4,5,6] #求均值 arr_mean = np.mean(arr) #求方差 arr_var = n...

使用python实现接口的方法

使用python实现接口的方法

接口基础知识: 简单说下接口测试,现在常用的2种接口就是http api和rpc协议的接口,今天主要说:http api接口是走http协议通过路径来区分调用的方法,请求报文格式都是ke...