码迷,mamicode.com
首页 > 编程语言 > 详细

Python:爬虫之利用Python获取指定网址上的所有图片—Jaosn niu

时间:2018-01-08 19:40:48      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:pre   int   div   code   gbk   get   取图   urlopen   网址   

# coding=gbk
import urllib.request
import re
import os
import urllib

def getHtml(url):  #指定网址获取函数
    page = urllib.request.urlopen(url)
    html = page.read()
    return html.decode(UTF-8)

def getImg(html):  #定义获取图片函数
    reg = rsrc="(.+?\.jpg)" pic_ext
    imgre = re.compile(reg)
    imglist = imgre.findall(html)
    x = 0
    path = rF:\File_Python\Crawler  
   # 将图片保存到F:\File_Python\Crawler文件夹中,如果没有Crawler文件夹,将会自动则创建
    if not os.path.isdir(path):  
        os.makedirs(path)  
    paths = path+\\     

    for imgurl in imglist:  #打开in集合中保存的imgurl图片网址,循环下载图片保存在本地
        urllib.request.urlretrieve(imgurl,{}{}.jpg.format(paths,x))   
        x = x + 1  
    return imglist
html = getHtml("https://tieba.baidu.com/p/2460150866?pn=10")#指定获取图片的网址路径
print (getImg(html)) 

 

Python:爬虫之利用Python获取指定网址上的所有图片—Jaosn niu

标签:pre   int   div   code   gbk   get   取图   urlopen   网址   

原文地址:https://www.cnblogs.com/yunyaniu/p/8244490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!