python+seleniumでスクレイピングをする (pubmedの検索結果を収集する)
やりたいこと
web上のページから情報を自動で回収したい (普通のスクレイピング) pubmedの検索結果を自動でまとめたい
方法
seleniumのインストール
pip install selenium
さらにブラウザに合うドライバをインストールする必要がある
例えばchoromeの場合は
ここのCurrent stable release からOSに合ったものをダウンロードする あとでパスを指定するのでわかりやすい場所に置いておくと良い
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless') #ヘッドレスモードを有効にすることで動作を早くする driver = webdriver.Chrome("./chromedriver_win32/chromedriver.exe",options=options) #先ほどダウンロードしたドライバのパスを指定 driver.get('https://pubmed.ncbi.nlm.nih.gov/') #pubmedのトップページを開く driver.find_element_by_id('id_term').send_keys('neuron') #neuronというキーワードで検索してみる driver.find_element_by_class_name('search-btn').click() # searchボタンをクリック papers = driver.find_elements_by_class_name('labs-docsum-title') 検索結果のタイトルリストを取得 for paper in papers: print(paper.text)
出力はこんな感じ
Neural coding: A single neuron's perspective. Single-neuron perturbations reveal feature-specific competition in V1. Tune it in: mechanisms and computational significance of neuron-autonomous plasticity. The neuron-level phenomena underlying cognition and consciousness: synaptic activity and the action potential. Evolution of Neuroglia. Mitostasis in Neurons: Maintaining Mitochondria in an Extended Cellular Architecture. [Neuron-glia interactions]. From form to function: the ways to know a neuron. Glia-neuron intercommunications and synaptic plasticity. Single-neuron theory of consciousness.
実際の検索結果