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数据预处理之数据标准化的几种处理方式

python数据预处理之数据标准化的几种处理方式

何为标准化: 在数据分析之前,我们通常需要先将数据标准化(normalization),利用标准化后的数据进行数据分析。数据标准化也就是统计数据的指数化。数据标准化处理主要包括数据同趋...

Python使用分布式锁的代码演示示例

Python使用分布式锁的代码演示示例

在计算机并发领域编程中总是会与锁打交道,锁又有很多种,互斥锁、自旋锁等等。 锁总是伴随着线程、进程这样的词汇出现,阮一峰有 一篇文章 对这些名词进行了简单易懂的解释。 我的理解是,使用线...

pytorch 修改预训练model实例

我就废话不多说了,直接上代码吧! class Net(nn.Module): def __init__(self , model): super(Net, self)._...

django 中QuerySet特性功能详解

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

python实现在windows下操作word的方法

本文实例讲述了python实现在windows下操作word的方法。分享给大家供大家参考。具体实现方法如下: import win32com from win32com.client...