python很强大一个很重要的特点就是有很海量的第三方库和包可以直接拿来使用,省去了很多不必要的工作,之前一直都是各种导包,“import ***”,一直没有时间去自己鼓捣一个库,尝试一下写库的乐趣,今天难得有一点时间,索性直接来实践一下吧,从零到一简单实现发布自己的第一个库——blogdownloader,地址在这里,截图如下:

项目非常的简单,主要做的就是一个优快云博文的下载器,输入博客ID就可以批量来下载博客的数据了,项目的功能模块目录结构如下所示:
├── ./blog
│ ├── ./blog/downloader.py
│ ├── ./blog/__init__.py
在整理得到上述的功能模块目录后就可以准备发布了,整体文件结构如下所示:
blogdownloaders
├── LICENSE
├── README.rst
├── requirements.txt
├── setup.py
├──blog
│ ├──__init__.py
│ └──downloader.py
LICENSE文件是授权文件,比如:MIT license, APACHE license
README.rst 文件想必大家都不陌生,其实就是项目介绍和使用说明
setup文件才是重点,是python模块安装所需要的文件,它的格式如下:
#!/usr/bin/env python
"""
Author: yishuihancheng
Copyright (c) 2011, Code for America. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. Redistributions in binary form
must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution. Neither the name of Code for America nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission. THIS SOFTWARE IS PROVIDED
BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
long_description="BlogDownloader"
setup(name="blogdownloader",
version="1.0",
description="Python wrapper for the BlogDownloader",
long_description=long_description,
keywords="blog,downloader,BlogDownloader",
author="yishuihancheng",
author_email="yishuihancheng@qq.com",
url="https://github.com/yishuihanhan/blog-downloader",
license="BSD",
packages=["blog"],
classifiers=['Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Topic :: Internet',
'Topic :: Internet :: WWW/HTTP',
]
)
其中的一些参数信息说明如下:
NAME 名字,一般放你包的名字即可
PACKAGES 包含的包,可以多个,这是一个列表
DESCRIPTION 关于这个包的描述
LONG_DESCRIPTION 参见read方法说明
KEYWORDS 关于当前包的一些关键字,方便PyPI进行分类。
AUTHOR 谁是这个包的作者,写谁的名字吧
AUTHOR_EMAIL 作者的邮件地址
URL 你这个包的项目地址,如果有,给一个吧,没有你直接填写在PyPI你这个包的地址也是可以的
VERSION 当前包的版本,这个按你自己需要的版本控制方式来
LICENSE 授权方式
INSTALL_REQUIRES 模块所依赖的python模块
文中的classifiers的内容并不是随便填写的,需要参照PyPI Classifiers来写
之后我们就可以开始准备打包了,打包之前为了保证正确性也可以自己去验证一下我们编写的安装脚本也就是setup.py是否是正确的,可以执行下面的一条命令:
python setup.py check
如果输出信息是error说明我们编写的安装脚本是包含错误信息的,如果是warning的信息的话建议对应位置修改一下,不修改也可以的,我这里就是没有修改的。当然如果没有任何显示信息那么表明我们编写的安装脚本是非常被Distutils所认可的,然后我们就可以开始打包之路了。
执行下面的打包命令:
python setup.py sdist
执行完成后,会在当前目录下生成dist目录和egg目录,截图如下所示:

目录结构示意图如下所示:
blogdownloaders
├── dist
│ └──dist/blogdownloader-1.0.tar.gz
├── LICENSE
├── README.rst
├── setup.py
├── blog
│ ├── blog/__init__.py
│ └── blog/downloader.py
├── blogdownloader.egg-info
│ ├── blogdownloader.egg-info/dependency_links.txt
│ ├── blogdownloader.egg-info/PKG-INFO
│ ├── blogdownloader.egg-info/requires.txt
│ ├── blogdownloader.egg-info/SOURCES.txt
│ └── blogdownloader.egg-info/top_level.txt
到此就可以结束我们的打包之旅了,如果我们想在本地进行安装的话可以在当前目录下执行下面的命令:
pip install .
这样就完成了包的安装。
接下来我们来实践一下在pypi网站将我们的包发布出去,首先我们打开 https://pypi.org/project/ ,这里是包发布可以查询的地方,我们先要查询一下自己包的名字在pypi社区里面是否已经存在了,存在的话我们就不能使用这个名字了,不存在的话我们可以直接发布的,网站截图如下所示:

当然,这里只是第一步,第二步是注册,这也是很重要的一步,跟Github类似,上传和维护项目的话是必须要进行开发者身份认证的,这里需要在PyPI · The Python Package Index这里完成注册,如下所示:

注册完成后需要进行邮箱邮件的验证,验证通过之后就可以进行上传发布了。
我们在运行命令上传的时候一直报错如下所示:

首先,我们保证密码是肯定正确的,错误也找了很多解决办法,很多博客都是说需要配置一个配置文件,如下:

但是亲测在我本机是不可行的,后来又找到了一种方式就是安装twine,然后执行下述命令:
twine upload dist/*
这次上传成功了,如下所示:

之后,我们打开pypi的项目查询框进行查询就可以查到我们发布的包了,就是本文刚开篇的时候的截图。我让别人尝试安装了这个模块是可以正常执行的,这里我在本机简单进行一下使用,如下所示:
#!usr/bin/env python
#encoding:utf-8
'''
__Author__:沂水寒城
功能: blogdownloader 模块使用
'''
from blog import downloader
def downloaderBlogsDemo(blog_name='Together_CZ',num=10,saveDir='blogPapers/'):
'''
优快云 博客文章下载Demo
blog_name:优快云博主的博客ID
num:下载多少页文章 >=2
saveDir:下载的html数据保存路径
'''
downloader.blogSpiders(blog_name=blog_name,num=num,saveDir=saveDir)
if __name__=='__main__':
downloaderBlogsDemo(blog_name='Together_CZ',num=2,saveDir='blogPapers/')
详细的说明都已经放在代码里面了就不多解释了,下面是结果输出信息:
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88538787
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88530709
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88420732
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88417488
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88408961
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88395718
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88243212
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88119461
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88088315
url: https://blog.youkuaiyun.com/Together_CZ/article/details/88028439
url: https://blog.youkuaiyun.com/Together_CZ/article/details/87982065
url: https://blog.youkuaiyun.com/Together_CZ/article/details/87808392
url: https://blog.youkuaiyun.com/Together_CZ/article/details/87619122
url: https://blog.youkuaiyun.com/Together_CZ/article/details/86735782
url: https://blog.youkuaiyun.com/Together_CZ/article/details/86690021
url: https://blog.youkuaiyun.com/Together_CZ/article/details/86689272
url: https://blog.youkuaiyun.com/Together_CZ/article/details/86678423
url: https://blog.youkuaiyun.com/Together_CZ/article/details/86678213
url: https://blog.youkuaiyun.com/Together_CZ/article/details/86640784
url: https://blog.youkuaiyun.com/Together_CZ/article/details/86629056
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
-23756800.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
-16384000.00%
-17203200.00%
-18022400.00%
-18841600.00%
-19660800.00%
-20480000.00%
-21299200.00%
-22118400.00%
-22937600.00%
[Finished in 15.9s]
下载下来的文章截图如下所示:

好了,讲到这里,我的第一个包发布之旅就结束了,感觉还是很惬意的,虽然说东西内容做得太简单了点,目的只是为了实践一下这个过程,记录一下。
最后,欢迎大家下载、安装使用哈,欢迎交流哈!

本文介绍了使用Python从零到一实现并发布自己的第一个库——blogdownloader的过程。该库是一个优快云博文下载器,可输入博客ID批量下载博客数据。文中详细说明了项目功能模块目录结构、打包、本地安装及在pypi网站发布的步骤,还分享了上传报错的解决办法。

被折叠的 条评论
为什么被折叠?



