美文网首页
Python写了个批量下载的小爬虫

Python写了个批量下载的小爬虫

作者: petry | 来源:发表于2016-09-12 17:04 被阅读47次

    !/usr/bin/python

    导入正则表达式库

    import re

    导入url相关库

    import urllib

    导入时间处理库

    import time

    获取网页源码的函数

    def getHtml(url):

    page = urllib.urlopen(url)

    html = page.read()

    return html

    从网页源码中获取图片地址 并且下载到指定目录

    def getImg(html):

    匹配图片地址 根据需要下载的网页的连接而定

    reg = r'src="(.*?.jpg)" alt'

    imgre = re.compile(reg)

    imglist = re.findall(imgre,html)

    now = int(time.time())

    fn = '/Users/istorm/Desktop/dwonload/'

    for imgurl in imglist:

    开始下载

    urllib.urlretrieve(imgurl,'%s%s.jpg' % (fn,now))

    now+=1

    print 'success'

    html = getHtml("http://www.apic.in/hentai/30053.htm")

    getImg(html)

    相关文章

      网友评论

          本文标题:Python写了个批量下载的小爬虫

          本文链接:https://www.haomeiwen.com/subject/gjipettx.html