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

yipeiwu_com4年前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调用c++ ctype list传数组或者返回数组的方法

示例1: pycallclass.cpp: #include <iostream> using namespace std; typedef unsigned char...

python去掉空白行的多种实现代码

python去掉空白行的多种实现代码

测试代码 jb51.txt 1:www.jb51.net 2:www.jb51.net 3:www.jb51.net 4:www.jb51.net 5:www.jb51.net 6...

MySQL中表的复制以及大型数据表的备份教程

表复制 mysql拷贝表操作我们会常常用到,下面就为您详细介绍几种mysql拷贝表的方式,希望对您学习mysql拷贝表方面能够有所帮助。 假如我们有以下这样一个表: id use...

TensorFlow实现RNN循环神经网络

TensorFlow实现RNN循环神经网络

RNN(recurrent neural Network)循环神经网络 主要用于自然语言处理(nature language processing,NLP) RNN主要用途是处理和预测序...

CentOS 6.5下安装Python 3.5.2(与Python2并存)

本文主要给大家介绍了关于CentOS 6.5 安装Python 3.5.2并与Python2并存的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: 安装步骤如下 1、准备编译环境...