Python爬蟲之網頁圖片

梓沐發表於2016-09-05

# -*- 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 = ""

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 = ""

# 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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章