LoginSignup
1
0

More than 5 years have passed since last update.

Windows更新プログラムの既知の問題をスクレイピングで取り出す(Python)

Posted at

環境

Python 3.7
BeautifulSoup
Selenium

対象ページ

MicrosoftのWindows更新プログラムのページから、「この更新プログラムの既知の問題」を取り出してみました。更新プログラムのページというのは例えば次のページです。
https://support.microsoft.com/ja-jp/help/4487044/windows-10-update-kb4487044

ページ内に次のような表がありますので、この「現象」と「回避策」の情報を取り出しています。

「この更新プログラムの既知の問題」の表

表の構造

ページのソースを確認したところ、該当のテーブルは次のようになっていました。
一つ目の<tr>がヘッダー行、二つ目以降が「現象」と「回避策」の内容です。

<table class="table ng-scope">
    <tbody>
        <tr>
            <td class="x-hidden-focus">現象</td>
            <td>回避策</td>
        </tr>
        <tr>
            <td>・・・</td>
            <td>・・・</td>
        </tr>

        ・・・

コード

上記の構造をもとに、1行目の最初のセルに「現象」とあれば対象の表と判断し、2行目以降を抽出しています。

from bs4 import BeautifulSoup
from selenium import webdriver

url = 'https://support.microsoft.com/ja-jp/help/4487044'

# Headlessモードで動かす
options = webdriver.chrome.options.Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(10)

driver.get(url)

tbodies = driver.find_elements_by_tag_name('tbody')
for tbody in tbodies:
    soup = BeautifulSoup(tbody.get_attribute('innerHTML'), 'html.parser')

    # ヘッダー行(1つ目の<tr>タグ)の内容で、取り出したい表かどうかを確認
    header = soup.find('tr').find('td')
    if header.text != '現象':
        continue

    # 2つ目以降の<tr>タグから現象と回避策の情報を抽出
    for row in soup.find_all('tr')[1:]:
        cells = row.find_all('td')
        print('===================')
        print('--- 現象 ---')
        print(cells[0].text)
        print('--- 回避策 ---')
        print(cells[1].text)
        print('')

driver.quit()

結果

===================
--- 現象 ---
この更新プログラムのインストール後、日本の元号名の最初の文字が省略形として認識されず、日付を解析する際に問題が発生することがあります。
--- 回避策 ---

日本の元号の 2 文字の省略形で、次のとおりレジストリを修正します。
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras]
"1868 01 01"="明治_明_Meiji_M"
"1912 07 30"="大正_大_Taisho_T"
"1926 12 25"="昭和_昭_Showa_S"
"1989 01 08"="平成_平_Heisei_H"
マイクロソフトは解決方法に取り組んでおり、今後のリリースで更新プログラムを提供します。

1
0
0

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
1
0