首页 > 解决方案 > 将抓取的图片保存到当前工作目录

问题描述

我抓取了一些图像,它询问要保存图像的文件夹的路径。但是,我希望将图像直接保存到当前工作目录。

def save():
            if not config.get('images'):
                _alert('No images to save')
                return

            if _save_method.get() == 'img':
                dirname = filedialog.askdirectory(mustexist=True)
                _save_images(dirname)

        def _save_images(dirname):
            i=1
            if dirname and config.get('images'):
                for img in config['images']:
                    img_data = requests.get(img['url']).content
                    filename = str(i)
                    i=i+1
                    with open(filename +'.png','wb') as f:
                        f.write(img_data)
                _alert('Done')

     _scrape_btn = ttk.Button(
            _mainframe, text='Scrape!', command=save
        )
        _scrape_btn.grid(row=2, column=0, sticky=W, pady=5)

标签: pythontkinterweb-scrapingweb-crawler

解决方案


您可以使用.或获取当前工作目录

import os
os.getcwd()

你能猜出它cwd代表什么吗:)


推荐阅读