SeleniumによるWebブラウザ自動化
Seleniumは、Webブラウザを自動化する強力なツールです。Webページへの移動、フォームへの入力、ボタンのクリック、データの抽出など、プログラムによってブラウザを制御できます。これは、Webスクレイピング、Webアプリケーションのテスト、反復的なタスクの自動化など、さまざまなタスクに役立ちます。
以下は、PythonでSeleniumを使用してCSDNブログをスクレイピングする基本的な例です。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time
def scrape_csdn_blog(url):
"""
Seleniumを使用してCSDNブログをスクレイピングし、ページソースからすべてのリンク(aタグ)を抽出します。
"https://blog.csdn.net/lzw_java/article"で始まるリンクをフィルタリングします。
Args:
url (str): CSDNブログのURL。
"""
try:
# ヘッドレスブラウジングのためのChromeオプションを設定します
chrome_options = Options()
chrome_options.add_argument("--headless") # ヘッドレスモードでChromeを実行します
chrome_options.add_argument("--disable-gpu") # GPUアクセラレーションを無効にします(ヘッドレスの場合推奨)
chrome_options.add_argument("--no-sandbox") # OSセキュリティモデルをバイパスします
chrome_options.add_argument("--disable-dev-shm-usage") # リソース制限の問題を克服します
# Chromeドライバを初期化します
driver = webdriver.Chrome(options=chrome_options)
# Webページを読み込みます
driver.get(url)
# すべての'a'タグ要素を見つけます
links = driver.find_elements(By.TAG_NAME, 'a')
if not links:
print("ページにリンクが見つかりません。")
driver.quit()
return
for link in links:
try:
href = link.get_attribute('href')
if href and href.startswith("https://blog.csdn.net/lzw_java/article"):
text = link.text.strip()
print(f"Text: {text}")
print(f"URL: {href}")
print("-" * 20)
except Exception as e:
print(f"リンクの抽出エラー: {e}")
continue
except Exception as e:
print(f"エラーが発生しました: {e}")
finally:
# ブラウザを閉じます
if 'driver' in locals():
driver.quit()
if __name__ == "__main__":
blog_url = "https://blog.csdn.net/lzw_java?type=blog" # 実際のURLに置き換えてください
scrape_csdn_blog(blog_url)