ITPub博客

首页 > 应用开发 > IT综合 > Python爬虫之网页图片

Python爬虫之网页图片

原创 IT综合 作者:梓沐 时间:2016-09-05 14:32:54 0 删除 编辑

# -*- coding:utf-8 -*-

# Author:sunw

import os

import re

import urllib.request

targetpath = "F:\\test"

def savepath(path):

    if not os.path.isdir(targetpath):

        os.mkdir(targetpath)

    pos = path.rindex('/')

    t = os.path.join(targetpath, path[pos + 1:])

    return t

url = "http://www.mmonly.cc/wmtp/wmbj/"

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '

                         'Chrome/51.0.2704.63 Safari/537.36'}

req = urllib.request.Request(url=url, headers=headers)

res = urllib.request.urlopen(req)

data = res.read()

for link, t in set(re.findall(r'(http[^\s]*?(jpg|png|gif))', str(data))):

    print(link)

    try:

        urllib.request.urlretrieve(link, savepath(link))

    except:

        print("mission falied")

# import urllib.request

#

#

# def savefile(data):

#     path = "F:\\test\\1.txt"

#     f = open(path, 'wb')

#     f.write(data)

#     f.close()

#

#

# url = "https://www.douban.com/"

# headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '

#                          'Chrome/51.0.2704.63 Safari/537.36'}

#

# req = urllib.request.Request(url, headers=headers)

# res = urllib.request.urlopen(req)

#

# data = res.read()

# savefile(data)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29812844/viewspace-2124509/,如需转载,请注明出处,否则将追究法律责任。

上一篇: redis安装
请登录后发表评论 登录
全部评论
擅长PLS/QL开发,SQL调优和改写,数据库设计

注册时间:2014-08-18

  • 博文量
    161
  • 访问量
    1085894