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

yipeiwu_com5年前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 含参构造函数实例详解

本篇博文主要介绍在Python3中如何构造含参构造函数 样例如下: class MyOdlHttp: username = '' password = '' def _...

python实现Adapter模式实例代码

python实现Adapter模式实例代码

本文研究的主要是python实现Adapter模式的相关内容,具体实现代码如下。 Adapter模式有两种实现方式一种是类方式。 #理解 #就是电源适配器的原理吧,将本来不兼容的接...

python进程和线程用法知识点总结

python进程和线程用法知识点总结

今天我们使用的计算机早已进入多CPU或多核时代,而我们使用的操作系统都是支持“多任务”的操作系统,这使得我们可以同时运行多个程序,也可以将一个程序分解为若干个相对独立的子任务,让多个子任...

python中的reduce内建函数使用方法指南

官方解释: Apply function of two arguments cumulatively to the items of iterable, from left to r...

SQLite3中文编码 Python的实现

读取十万多条文本写入SQLite类型数据库,由于文本中存在中文字符,插入到数据库没错,取出时一直是UnicodeDecodeError,导致折腾了一天。 最后的解决方法: Python连...