seleniumを使ったスクレイピング(Python)
手順
seleniumのインストール
pip install selenium
WebDriverのインストール
Seleniumのサイトから各種ブラウザのWebDriverをダウンロードすることができます。
Google Chrome Driverを使いましたが、 自動テストにはPhantomJSの方が便利かもしれません。
Downloads - ChromeDriver - WebDriver for Chrome
使う
ボタン押下などの操作を記述することもできます。
driver = webdriver.Chrome(ダウンロードしたWebDriverのパス) # Web APIを使ってDOMアクセスする # driver.open(アクセスする対象URL) # elem = driver.find_element_by_id(ID) # テキスト要素取得 #elem.text # ブラウザを閉じる driver.quit()
Tips
XPathの書き方
# ドキュメントルートから要素を検索します driver. find_elements_by_xpath(‘//div[@class=“container”]) element = driver.find_element_by_id(‘top’) # 子要素から対象のXPath要素を検索します element.find_elements_by_xpath(‘.//div[@class=“container”]) # ドキュメントルートから対象のXPath要素を検索します element.find_elements_by_xpath(‘//div[@class=“container”])
要素がない場合の処理
要素がない場合は、NoSuchElementExceptionがストーされるので処理します。
# 例外のimportが必要 from selenium.common.exceptions import NoSuchElementException try: elemement = driver.find_element_by_class_name(クラス名) 処理 except NoSuchElementException: 例外発生時の処理
スクリーンショット
エビデンスを残す場合に便利です。不具合が発生した時に確認するのには便利ですが、開発時にチェックするのは…あれですね。
driver.save_screenshot(“scenario_1-1")
ソース取得
回帰テストに使えます。
driver. page_source
リンク
ファイル保存時にディレクトリがなければ作成する(Python)
ファイル保存時にディレクトリがなければ作成する処理は以下のように書く。
def save_text(path, text): # ディレクトリが存在しなければ作成する path_dir = os.path.dirname(path) if not os.path.exists(path_dir): # makedirsは途中のディレクトがない場合作成します os.makedirs(path_dir) # ファイル保存(上書き) f = open(path,'w') f.write(text) f.close()
リンク
MacOSXにMecabインストール
fasttextを試すため、Mac OS X(Sierra)にMecabをインストールした。
pythonのバージョンは3.5.2
$ python --version Python 3.5.2 :: Anaconda 4.1.1 (x86_64)
手順
mecab-python3をインストール
pip install mecab-python3
mecab+ipadicをインストール
brew install mecab-ipadic
辞書(mecab-ipadic-NEologd)もインストールした。
多数のWeb上の言語資源から得た新語を追加することでカスタマイズした MeCab 用のシステム辞書
ネット上で流行した単語や慣用句やハッシュタグをエントリ化したデータ
実行
$ mecab -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd ピコ太郎 ピコ太郎 名詞,固有名詞,人名,一般,*,*,ピコ太郎,ピコタロウ,ピコタロー
$ python Python 3.5.2 |Anaconda 4.1.1 (x86_64)| (default, Jul 2 2016, 17:52:12) [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import MeCab >>> mecab = MeCab.Tagger() >>> print(mecab.parse("ピコ太郎")) ピコ太郎 名詞,固有名詞,人名,一般,*,*,ピコ太郎,ピコタロウ,ピコタロー EOS
Links
Visual Studio CodeでRubyデバッグを試してみました
Visual Studio CodeによるRubyのデバッグ | Developers.IOを参考にVisual Studio CodeでRubyのデバッグを試してみました。ウオッチ式、コールスタックが使えるので十分実用できそうです。
今まで、Rubyのデバッグ用にRubyMineを購入していたが、Visual Studio Codeで十分な気がしてきました。
Xcode8でコメント作成
Xcode8では、option + command + /で関数のコメントのひな形をを作成してくれます。便利。
リンク
UILabelのタップイベントを処理する
UILabelのタップイベントを処理して、ラベルに表示する値をアラートダイアログで入力する方法です。
UIラベルのisUserInteractionEnabledをtrueにして、touchesEndedで処理します。
class ViewController: UIViewController { let timerLabelTag = 10 @IBOutlet weak var timerLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() timerLabel.tag = timerLabelTag timerLabel.isUserInteractionEnabled = true } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { for touch: UITouch in touches { let tag = touch.view!.tag if (tag == timerLabelTag) { // アラートダイアログで入力する let alert = UIAlertController(title: "タイマー時間セットアップ", message: "時間(秒)を入力してください", preferredStyle: .alert) let saveAction = UIAlertAction(title: "セット", style: .default) { (action:UIAlertAction!) -> Void in let textField = alert.textFields![0] as UITextField timerLabel = textField.text! } } let cancelAction = UIAlertAction(title: "キャンセル", style: .default) { (action:UIAlertAction!) -> Void in } // UIAlertControllerにtextFieldを追加 alert.addTextField { (textField:UITextField!) -> Void in } alert.addAction(saveAction) alert.addAction(cancelAction) present(alert, animated: true, completion: nil) } } } }