Skip to content

chingx/crawler_click_tutorial

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crawler_click_tutorial

今天教大家用 python 建立屬於自己的 command line tool 😏

由於最近剛好看到 click 這個東西,所以決定要拿之前的小程式來把玩一下 😄,

本範例使用之前所寫的簡單爬蟲 eynyCrawlerMega

特色

  • 抓取 eyny 電影區 Mega and Google 文章連結

輸出格式

  • 文字檔 ( 爬蟲結束後會自動開啟文字檔 )

click 教學

安裝 click

pip install click

官方範例 demo

hello.py 程式碼如下

import click

@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo('Hello %s!' % name)

if __name__ == '__main__':
    hello()

為什麼要用 click.echo() 而不是 print()呢 ?

因為這樣可以解決一些編碼上的問題 ( 詳細請看 click 的 source code )。

然後我們可以這樣使用

python hello.py --count=3

python hello.py --help

建立 command line tool

接下來介紹建立屬於自己的 command line tool,

可以參考我的 setup.py ,程式碼如下

from setuptools import setup

setup(
    name='crawler_click_tutorial',
    version='0.1',
    py_modules=['crawler_click_tutorial'],
    install_requires=[
        'Click',
        'requests',
        'beautifulsoup4',
    ],
    entry_points='''
        [console_scripts]
        movie=crawler_click_tutorial:cli
    ''',
)

最後只需要在 cmd ( 命令提示字元 ) 執行

pip install --editable .

注意,最後有個 .

以後,只要在 cmd ( 命令提示字元 ) 輸入 movie 即可 !!

執行畫面

首先,讓我們先來看看有什麼指令可以使用

movie --help

movie

超酷 😆 ,還有進度條 😮

最後會自動幫你打開文件

movie --page=5 --output=output_file.txt

簡單防呆機制

movie --page=11

結論

click 真的超讚 👍 , 可以利用它打造出屬於自己的 command line tool ,

不管是娛樂還是加速自己的工作流程都非常方便。

Environment

Python 3.5.3

Reference

License

MIT license

About

click tutorial ( crawler ) use python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%