脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现简单的获取图片爬虫功能示例

Python实现简单的获取图片爬虫功能示例

2020-11-25 00:32微烟波 Python

这篇文章主要介绍了Python实现简单的获取图片爬虫功能,涉及Python使用urllib模块及正则模块操作页面元素获取图片的相关技巧,需要的朋友可以参考下

本文实例讲述了Python实现简单的获取图片爬虫功能。分享给大家供大家参考,具体如下:

简单Python爬虫,获得网页上的照片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#coding=utf-8
import urllib
import re
def getHtml(url):
  page = urllib.urlopen(url)
  html = page.read()
  return html
def getImg(html):
  reg = r'src="(.+?\.jpg)" pic_ext'
  imgre = re.compile(reg)
  imglist = re.findall(imgre, html)
  return imglist
// 网站地址
url = "http://tieba.baidu.com/p/3368048910?pn=2"
html = getHtml(url)
listimg = getImg(html)
x = 0
for imgAddress in listimg:
  print imgAddress
  urllib.urlretrieve(imgAddress, 'image%s.jpg' % x)
  x+=1

希望本文所述对大家Python程序设计有所帮助。

延伸 · 阅读

精彩推荐