LoginSignup
64
90

More than 1 year has passed since last update.

Google Colaboratory(Colab)でスクレイピングするときのチートシート

Last updated at Posted at 2020-03-16

目次

#Beautiful Soupの使用方法

文字化け解消方法

requestsを使用する場合は通常下記のように書くと思いますが、

from bs4 import BeautifulSoup
import requests

res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')

これだと文字化けするサイトがあるので、下記にすると文字化けがかなり解消できる。

from bs4 import BeautifulSoup
import requests

res = requests.get(url)
soup = BeautifulSoup(res.content, "lxml", from_encoding='utf-8')

findのコード一覧

説明 コード例
1件検索 soup.find('li')
タグ全検索 soup.find_all('li')
属性検索 soup.find('li', href='html://www.google.com/')
複数要素を取得 soup.find_all(['a','p'])
id検索 soup.find('a', id="first")
class検索 soup.find('a', class_="first")
属性取得 first_link_element['href']
テキスト検索 soup.find('dt' ,text='検索ワード')
テキスト部分一致の検索 soup.find('dt' ,text=re.compile('検索ワード'))
親要素を取得 .parent
次の要素を1件取得 .next_sibling
次の要素を全件取得 .next_siblings
前の要素を1件取得 .previous_sibling
前の要素を全件取得 .previous_siblings
テキスト要素の取得 .string

selectのコード一覧

説明 コード例
1件検索 soup.select_one('cssセレクタ')
全件検索 soup.select('cssセレクタ')

セレクタの指定方法一覧

説明 コード例
id検索 soup.select('a#id')
class検索 soup.select('a.class')
classの複数検索 soup.select('a.class1.class2')
属性検索1 soup.select('a[class="class"]')
属性検索2 soup.select('a[href="http://www.google.com"]')
属性検索3 soup.select('a[href]')
子要素の取得 soup.select('.class > a[href]')
子孫要素の取得 soup.select('.class a[href]')
n番目の要素の取得 soup.select('.a:nth-of-type(n)')

属性要素は検索したい要素に応じて変更する。
id,class,href,name,summaryなど。
子要素(1階層下)だけ取得したい場合は>を挟み、子孫要素(階層下全て)を取得したい場合はスペースを入れる。

#Seleniumの使用方法

##Seleniumを使うための事前準備

Colabで使用する際はSeleniumのダウンロードとUIの仕様ができないので、
その設定が必要になります。

# Seleniumを使用するのに必要なライブラリをダウンロード
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium

from selenium import webdriver

# ドライバーをUIなしで使用するための設定
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',chrome_options=options)
driver.implicitly_wait(10)

2022年12月
久しぶりに使おうと思ったらwebdriver.Chromeのオプション引数が
optionsからchrome_optionsに変わっていました。

SeleniumとBeautiful Soupを使用する場合

使用ケースとしては、Beautiful Soupだけではどうしても要素が取得できない場合に
seleniumuでページを読み込んでからBeautiful Soupで必要な情報を抜きたい場合。

driver.get(url)
html = driver.page_source.encode('utf-8')
soup = BeautifulSoup(html, 'html.parser')

Seleniumの基本コード

説明 コード例
URLを開く driver.get('URL')
一つ前に戻る driver.back()
一つ前に進む driver.forward()
ブラウザを更新 driver.refresh()
現在のURLを取得 driver.current_url
現在のタイトルを取得 driver.title
現在のウィンドウを閉じる driver.close()
全てのウィンドウを閉じる driver.quit()
クラスで要素取得 driver.find_element_by_class_name('classname')
IDで要素取得 driver.find_element_by_id('id')
XPATHで要素取得 driver.find_element_by_xpath('xpath')
XPATHでテキスト検索 driver.find_element_by_xpath('//*[text()="strings"]')
XPATHでテキスト部分一致検索 driver.find_element_by_xpath('//*[contains(text(), "strings")]')
要素をクリック driver.find_element_by_xpath('XPATH').click()
テキスト入力 driver.find_element_by_id('ID').send_keys('strings')
テキストの取得 driver.find_element_by_id('ID').text
属性の取得(hrefの場合) driver.find_element_by_id('ID').get_attribute('href')
要素が表示されているか判定 driver.find_element_by_xpath('xpath').is_displayed()
要素が有効か判定 driver.find_element_by_xpath('xpath').is_enabled()
要素が選択されているか判定 driver.find_element_by_xpath('xpath').is_selected()

ドロップダウンを選択したいとき

from selenium.webdriver.support.ui import Select

element = driver.find_element_by_xpath("xpath")
Select(element).select_by_index(indexnum) # indexで選択
Select(element).select_by_value("value") # valueの値
Select(element).select_by_visible_text("text") # 表示テキスト

Xpathの指定方法一覧

説明 コード例
全ての要素を選択 //*
全ての要素を選択 //a
属性を選択 @href
複数要素を選択 [a or h2]
idで要素取得 //*[@id="id"]
classで要素取得 //*[@class="class"]
テキスト検索 //*[text()="strings"]
テキストの部分検索 //*[contains(text(), "strings")]
クラスの部分一致 //*contains(@class, "class")
次のノード取得 /following-sibling::*[1]
2つあとのa要素の /following-sibling::a[2]
後ろのノード取得 /preceding-sibling::*[1]

その他ノードの取得方法はこちらを参考にする

タブを変更する場合

クリックするとページ遷移せず新しいタブができる場合に使用

handle_array = driver.window_handles
driver.switch_to.window(handle_array[1])

特定の要素が表示されるまで待機処理


from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# ページ上のすべての要素が読み込まれるまで待機(15秒でタイムアウト判定)
WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)

# ID指定したページ上の要素が読み込まれるまで待機(15秒でタイムアウト判定)
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.ID, 'ID名')))

# CLASS名指定したページ上の要素が読み込まれるまで待機(15秒でタイムアウト判定)
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.CLASS_NAME, 'CLASS名')))

# XpathでCLASS名指定したページ上の要素が読み込まれるまで待機(15秒でタイムアウト判定)
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, 'xpath')))

クリックできないときの処理

target = driver.find_element_by_xpath('xpath')
driver.execute_script("arguments[0].click();", target)

Pandasを使用してデータを保存する方法

データフレームを作成してデータを追加していく方法

import pandas as pd
columns = ['項目1', '項目2', '項目3', '項目4', '項目5']
df = pd.DataFrame(columns=columns)

# データ取得の処理
se = pd.Series([data1, data2, data3, data4, data5], columns)
df = df.append(se, columns)

上記のようにデータフレームを作成し後、データを保存する方法には下記の3パターンがあります。

① CSVでダウンロードする
② マイドライブに格納
③ スプレッドシートに反映させる

① PandasのデータCSVでダウンロードする

from google.colab import files

filename = 'filename.csv'
df.to_csv(filename, encoding = 'utf-8-sig') 
files.download(filename)

② Pandasのデータをマイドライブに格納する

from google.colab import drive

filename = 'filename.csv'
path = '/content/drive/My Drive/' + filename

with open(path, 'w', encoding = 'utf-8-sig') as f:
  df.to_csv(f)

③ Pandasのデータをスプレッドシートに反映させる

!pip install gspread

from google.colab import auth
from oauth2client.client import GoogleCredentials
import gspread
import pandas as pd

# 認証処理
auth.authenticate_user()
gc = gspread.authorize(GoogleCredentials.get_application_default())

ss_id = 'スプレッドシートのID'
workbook = gc.open_by_key(ss_id)

# パラメーターを指定してシートへ張り付ける場合
workbook.values_update(
  # sheet!A1などセルを指定
  target_cell,
  params={
      'valueInputOption': 'USER_ENTERED'
  },
  body={
      'values': [df.columns.values.tolist()] + df.values.tolist()
  }
)

【参考サイト】
BeautifulSoup4のチートシート(セレクターなど)
Python3メモ - BeautifulSoup4のあれこれ
WebスクレイピングのためのCSSセレクタの基本
Selenium webdriverよく使う操作メソッドまとめ
XPathとは?XPathの基本知識を学ぼう!
Webスクレイピングに不可欠!Xpathのまとめ
gspreadライブラリの使い方まとめ!Pythonでスプレッドシートを操作する

64
90
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
64
90