python抓取网页中的图片示例

WBOY
Release: 2016-06-16 08:45:00
Original
957 people have browsed it

复制代码 代码如下:

#coding:utf8
import re
import urllib
def getHTML(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

def getImg(html,imgType):
    reg = r'src="(.*?\.+'+imgType+'!slider)" '
    imgre = re.compile(reg)
    imgList = re.findall(imgre, html)
    x=0
    for imgurl in imgList:
        print imgurl
        urllib.urlretrieve(imgurl, '%s.%s' % (x, imgType))
        x =x+1


html= getHTML("http://www.jb51.net")

getImg(html,'jpg')

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!