Skip to main content

A wrapper for Selenium WebDriver that simplifies browser automation.

Project description

quickdriver

Overview - 概要

quickdriver is a wrapper for Selenium. It simplifies browser automation, web scraping, data saving, and other tasks by providing an easy-to-use interface to WebDriver.

quickdriverはSeleniumのラッパーです。QuickDriverを介してWebDriverを操作することで、ブラウザの自動操作、スクレイピング、データ保存などの処理を簡単に実装できます。

Installation - インストール

You can install quickdriver and all the libraries needed to run it using pip:

quickdriverとその実行に必要な全てのライブラリは以下のコマンドでインストールできます。

pip install quickdriver

Requirements - 必要条件

To run quickdriver, you need the following environment:

quickdriverの実行には、以下の環境が必要です。

  • Python 3.8 or higher
  • Libraries:
    • pandas (version 2.2.3 or higher)
    • selenium (version 4.27.1 or higher)
    • tqdm (version 4.67.1 or higher)
    • pyarrow (version 16.1.0 or higher)

Usage Example - 使用例

import random

from selenium import webdriver as wd
from quickdriver import QuickDriver

options = wd.ChromeOptions()
options.add_argument('--incognito') # secret mode
# options.add_argument('--headless=new') # headless mode
options.add_argument('--start-maximized') # maximize a window
options.add_experimental_option('prefs', {'profile.managed_default_content_settings.images': 2}) # Image loading disabled
# options.add_argument(r'--user-data-dir=C:\Users\xxxx\AppData\Local\Google\Chrome\User Data') # User profile destination path
# options.add_argument('--profile-directory=Profile xx') # User profile directory name

with wd.Chrome(options=options) as driver:   
    d = QuickDriver(driver)
    
    @d.crawl(delay=1.25)
    def prefectures():
        return (d.attr('href', e) for e in d.ss('li.item > ul > li > a'))
        
    @d.crawl(delay=lambda: random.choice([0, 1, 2]))
    def each_classroom():
        return (d.attr('href', e) for e in d.ss('.school-area h4 a'))
    
    @d.crawl(delay=lambda: random.uniform(0, 2))
    def scrape_classroom_info():
        d.save_row('./classroom_info', {
            'URL': driver.current_url,
            '教室名': d.attr('textContent', d.s('h1 .text01')),
            '住所': d.attr('innerText', d.s('.item .mapText')),
            '電話番号': d.attr('textContent', d.s('.item .phoneNumber')),
            'HP': d.attr('href', d.s('a', d.next(d.s_re('th', 'ホームページ')))),
        })
    
    scrape_classroom_info(each_classroom(prefectures(['https://www.foobarbaz1.jp'])))

Basic Usage - 基本的な使い方

QuickDriver Class

The quickdriver module consists of a single class: QuickDriver. This class wraps a Selenium WebDriver instance, providing convenient methods for interacting with web pages.

quickdriverモジュールは、QuickDriverクラス1つによって構成されています。QuickDriverクラスは、WebDriverのインスタンスを受け取ってSeleniumの処理をラップします。

d = QuickDriver(driver)

Methods

The QuickDriver class provides the following instance methods:

QuickDriverクラスは、以下のインスタンスメソッドによって構成されています。

1. ss

Get multiple web elements as a list using a CSS selector. Returns an empty list if no elements are found. If a WebElement is passed as the second argument, the search is performed within that element's DOM subtree.

セレクタで複数のWeb要素をリストで取得します。存在しない場合は空のリストを返します。第二引数にWeb要素を渡すと、その要素のDOMサブセットからの取得となります。

elems = d.ss('li.item > ul > li > a')

2. s

Get a single web element using a CSS selector. If more than one element satisfies the condition, only the first one is returned. Returns None if no element is found. If a WebElement is passed as the second argument, the search is performed within that element's DOM subtree.

セレクタでWeb要素を取得します。条件を満たす要素が複数ある場合、最初の一つだけが返されます。存在しない場合はNoneを返します。第二引数にWeb要素を渡すと、その要素のDOMサブセットからの取得となります。

elem = d.s('h1 .text01')

3. ss_re

Get multiple web elements as a list using a CSS selector and a regular expression to match the element's textContent. Returns an empty list if no elements are found. If a WebElement is passed as the third argument, the search is performed within that element's DOM subtree.

セレクタと、textContentに対する正規表現マッチングで複数のWeb要素をリストで取得します。存在しない場合は空のリストを返します。第三引数にWeb要素を渡すと、その要素のDOMサブセットからの取得となります。

elems = d.ss_re('li.item > ul > li > a', r'店\s*舗')

4. s_re

Get a single web element using a CSS selector and a regular expression to match the element's textContent. If more than one element satisfies the condition, only the first one is returned. Returns None if no element is found. If a WebElement is passed as the third argument, the search is performed within that element's DOM subtree.

セレクタと、textContentに対する正規表現マッチングでWeb要素を取得します。条件を満たす要素が複数ある場合、最初の一つだけが返されます。存在しない場合はNoneを返します。第三引数にWeb要素を渡すと、その要素のDOMサブセットからの取得となります。

elem = d.s_re('table tbody tr th', r'住\s*所')

5. attr

Get the value of an attribute from a web element.

Web要素から任意の属性値を取得します。

text = d.attr('textContent', elem)

6. next

Get the next sibling element of a web element.

渡されたWeb要素の弟要素を取得します。

next_elem = d.next(elem)

7. go_to

Navigate to the specified URL.

指定したURLに遷移します。

d.go_to('https://foobarbaz1.com')

8. click

After removing the target attribute of the specified web element, the click event is fired.

指定したWeb要素のtarget属性を削除した後、clickイベントを発生させます。

d.click(elem)

9. switch_to

Switch the driver's focus to the specified iframe element.

指定したiframe要素内に制御を移します。

d.switch_to(iframe_elem)

10. scroll_to_view

Scroll the page to bring the specified web element into view.

指定したWeb要素をスクロールして表示します。

d.scroll_to_view(elem)

11. save_row

Add a row to a table (creates the table if it doesn't exist) and save it as a Parquet file. The table name is determined by the provided path.

パス形式の名前で指定したテーブルデータ(無い場合は作成されます)に行を追加し、Parquetファイルとして保存します (拡張子の記述は不要)。

d.save_row('./scrape/foo', {
    '列名1': text01,
    '列名2': text02,
    '列名3': text03,
})

12. progress

Display a progress bar for a function that iterates over a list of URLs.

urlリストの各ページに対して処理を行っていく関数の進捗状況を表示します。

for page_url in d.progress(page_urls, func):
    d.go_to(page_url)
    func()

13. crawl

Decorator. The granted function will take a list of URL strings as arguments; passing a URL list will cause the function to access the URLs in order, executing the function's processing on each page. delay is the number of seconds between requests. If the function returns a list, set, generator, etc., the final return value is a list of all of them combined.

デコレータ。付与された関数は、URL文字列のリストを引数として受け取るようになります。URLリストを渡すと、そのURLに順番にアクセスしていき、各ページに対して関数の処理を実行するようになります。delayはリクエスト間隔の秒数です。関数の処理がリスト、集合、ジェネレータ等を返す場合、最終的な戻り値はそれら全てを結合したリストとなります。

@d.crawl(delay=1.25)
def foo():
    # 略

@d.crawl(delay=lambda: random.uniform(0, 2))
def bar():
    # 略

License - ライセンス

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quickdriver-2.0.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quickdriver-2.0.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file quickdriver-2.0.0.tar.gz.

File metadata

  • Download URL: quickdriver-2.0.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for quickdriver-2.0.0.tar.gz
Algorithm Hash digest
SHA256 45ed89afec0b72acc53205adb2a8bad2d374e16d8fb38a422879c3dc2e2f5466
MD5 db802e8c97712ac80bac1e63f61d4f40
BLAKE2b-256 75eb493b3bc09d7622cc724bb28ad727a702dd4865c50f7fc55ab1c23ef1e8a5

See more details on using hashes here.

File details

Details for the file quickdriver-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: quickdriver-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for quickdriver-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a066e0dc393405384e55eee02da0d0d94aa5390cd0f159d037959a26a8db6f9c
MD5 c0880376c1c5edd90f118193375b7d05
BLAKE2b-256 872555ba9214dae41e75221642fc893b58f331a7c64de0523e1dbe05bf4fd2d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page