2019年10月29日火曜日

開発環境

退屈なことはPythonにやらせよう ―ノンプログラマーにもできる自動化処理プログラミング (Al Sweigart(著)、相川 愛三(翻訳)、オライリージャパン)の第Ⅱ部(処理の自動化)、11章(Webスクレイピング)、11.5(Google検索)のコードを修正して動くようにしたプログラムに、コマンドライン引数でサイト数を指定できる機能を追加してみた。

コード

Python 3

#!/usr/bin/env python3
import sys
import webbrowser
import requests
import bs4
from typing import List


def get_bequatifulsoup(url: str) -> bs4.BeautifulSoup:
    res = requests.get(url)
    res.raise_for_status()
    return bs4.BeautifulSoup(res.text, 'lxml')


def get_links_google(keyword: str, n: int) -> List[str]:
    soup = get_bequatifulsoup(f'https://google.com/search?q={keyword}')
    links = soup.select('a')
    i: int = 0
    result: List[str] = []
    for link in links:
        href = link.get('href')
        if href.startswith('/url?q='):
            result.append(f'https://google.com{href}')
            i += 1
            if i == n:
                break
    return result


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('-n', '--number', help="how many",
                        type=int, choices=range(1, 11))
    parser.add_argument('keyword', help='search keyword',
                        type=str, nargs='+')
    args = parser.parse_args()
    n: int = 5
    if args.number is not None:
        n = args.number
    keywords: str = ' '.join(args.keyword)
    try:
        print(f'searching {keywords} …')
        links: List[str] = get_links_google(keywords, n)
    except Exception as err:
        print(err)
    else:
        for link in links:
            webbrowser.open(link)

入出力結果(Zsh、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))

% ./ksearch.py 
usage: ksearch.py [-h] [-n {1,2,3,4,5,6,7,8,9,10}] keyword [keyword ...]
ksearch.py: error: the following arguments are required: keyword
% ./ksearch.py -h
usage: ksearch.py [-h] [-n {1,2,3,4,5,6,7,8,9,10}] keyword [keyword ...]

positional arguments:
  keyword               search keyword

optional arguments:
  -h, --help            show this help message and exit
  -n {1,2,3,4,5,6,7,8,9,10}, --number {1,2,3,4,5,6,7,8,9,10}
                        how many
% ./ksearch.py --help
usage: ksearch.py [-h] [-n {1,2,3,4,5,6,7,8,9,10}] keyword [keyword ...]

positional arguments:
  keyword               search keyword

optional arguments:
  -h, --help            show this help message and exit
  -n {1,2,3,4,5,6,7,8,9,10}, --number {1,2,3,4,5,6,7,8,9,10}
                        how many
% ./ksearch.py python 
searching python …
% ./ksearch.py -n -1 golang
usage: ksearch.py [-h] [-n {1,2,3,4,5,6,7,8,9,10}] keyword [keyword ...]
ksearch.py: error: argument -n/--number: invalid choice: -1 (choose from 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
% ./ksearch.py -n 11 golang
usage: ksearch.py [-h] [-n {1,2,3,4,5,6,7,8,9,10}] keyword [keyword ...]
ksearch.py: error: argument -n/--number: invalid choice: 11 (choose from 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
% ./ksearch.py -n 1 golang
searching golang …
% ./ksearch.py -n 2 python golang
searching python golang …
%

0 コメント:

コメントを投稿