LoginSignup
8
5

More than 5 years have passed since last update.

PythonでLet's Webスクレイピング! ②任意の属性値の抽出

Last updated at Posted at 2018-10-08

はじめに

PythonでWebスクレイピングやってみようの第2弾です。
第1弾は以下からご覧下さい。

PythonでLet's Webスクレイピング! ①任意の要素の抽出

今回も私のQiitaのマイページ(https://qiita.com/chanmaru)
を使ってスクレイピングしていきます!

※処理環境※

本記事に記載のコードは以下の環境で動作検証をしています。
OS:Windows10
Python:3.6.5
(beautifulsoup4:4.6.3)

複数の要素を抽出する

私のQiitaのマイページから全てのa要素の値を抽出してみます。
※マイページURL:https://qiita.com/chanmaru
複数の要素を抽出するにはfind_all()メソッドを使います。

import urllib.request
from bs4 import BeautifulSoup

url = "https://qiita.com/chanmaru"
res = urllib.request.urlopen(url)
soup = soup = BeautifulSoup(res, 'html.parser')

a_list = soup.findAll("a")
for a in a_list:
    print(a.string)

結果は以下のとおりになります。(2018.10.08時点)

None
3
None
None
None
4
None
None
None
None
Python:処理ファイルをGUIから選択する方法
Python
python3
Pythonでファイル処理のGUIプログラムを作ってみた!
Python
python3
PythonでLet's Webスクレイピング! ①任意の要素の抽出
Python
python3
BeautifulSoup
Webスクレイピング
PostGISで地理空間情報を管理してみた!
PostgreSQL
PostGIS
QGIS
None
None
None
None
Liked Items
Comments
None
chanmaru
PythonでLet's Webスクレイピング! ①任意の要素の抽出
Python
python3
BeautifulSoup
Webスクレイピング
None
chanmaru
Pythonでファイル処理のGUIプログラムを作ってみた!
Python
python3
2
None
chanmaru
PostGISで地理空間情報を管理してみた!
PostgreSQL
PostGIS
QGIS
None
chanmaru
Python:処理ファイルをGUIから選択する方法
Python
python3
© 2011-2018 Increments Inc.
Terms
Guideline
Privacy
Help
About
Users
Tags
Items
Blog
API
Team
None
here

コードのfind_all()メソッドはリストを出力するので、for文を用いて1要素ずつ値を出力しています。


a_list = soup.findAll("a")
for a in a_list:
    print(a.string)

任意の属性値の抽出

先ほどのコードでa要素を出力したので、その中からURLを指定するhref属性値を抽出してみます。

これにより、私のQiitaのマイページ(https://qiita.com/chanmaru)
から遷移するURLを抽出することができます。

href属性はattrsプロパティを使います。

コードはこんな感じです。

import urllib.request
from bs4 import BeautifulSoup

url = "https://qiita.com/chanmaru"
res = urllib.request.urlopen(url)
soup = soup = BeautifulSoup(res, 'html.parser')

a_list = soup.findAll("a")
for a in a_list:
    href = a.attrs['href']
    print(a.string, '-->', href)

結果は以下のとおりです。(2018.10.08時点)

None --> https://twitter.com/chanmaru911002
3 --> /chanmaru/following_tags
None --> /tags/PHP
None --> /tags/Twitter
None --> /tags/Python
4 --> /chanmaru/following_users
None --> /turmericN
None --> /mikan3rd
None --> /hiro_matsuno2
None --> /dev-sabatarou
Python:処理ファイルをGUIから選択する方法 --> /chanmaru/items/1b64aa91dcd45ad91540
Python --> /tags/python
python3 --> /tags/python3
Pythonでファイル処理のGUIプログラムを作ってみた! --> /chanmaru/items/8e5ebf7d8b0b21c8fd3a
Python --> /tags/python
python3 --> /tags/python3
PythonでLet's Webスクレイピング! ①任意の要素の抽出 --> /chanmaru/items/b6c1fa93814bd1
e32601
Python --> /tags/python
python3 --> /tags/python3
BeautifulSoup --> /tags/beautifulsoup
Webスクレイピング --> /tags/web%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0
PostGISで地理空間情報を管理してみた! --> /chanmaru/items/0cb67455c294943ae649
PostgreSQL --> /tags/postgresql
PostGIS --> /tags/postgis
QGIS --> /tags/qgis
None --> /chanmaru
None --> /chanmaru/contributions
None --> /chanmaru/followers
None --> /chanmaru
Liked Items --> /chanmaru/like
Comments --> /chanmaru/comments
None --> /chanmaru
chanmaru --> /chanmaru
PythonでLet's Webスクレイピング! ①任意の要素の抽出 --> /chanmaru/items/b6c1fa93814bd1
e32601
Python --> /tags/Python
python3 --> /tags/python3
BeautifulSoup --> /tags/BeautifulSoup
Webスクレイピング --> /tags/Web%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0
None --> /chanmaru
chanmaru --> /chanmaru
Pythonでファイル処理のGUIプログラムを作ってみた! --> /chanmaru/items/8e5ebf7d8b0b21c8fd3a
Python --> /tags/Python
python3 --> /tags/python3
2 --> /chanmaru/items/8e5ebf7d8b0b21c8fd3a
None --> /chanmaru
chanmaru --> /chanmaru
PostGISで地理空間情報を管理してみた! --> /chanmaru/items/0cb67455c294943ae649
PostgreSQL --> /tags/PostgreSQL
PostGIS --> /tags/PostGIS
QGIS --> /tags/QGIS
None --> /chanmaru
chanmaru --> /chanmaru
Python:処理ファイルをGUIから選択する方法 --> /chanmaru/items/1b64aa91dcd45ad91540
Python --> /tags/Python
python3 --> /tags/python3
© 2011-2018 Increments Inc. --> http://increments.co.jp
Terms --> https://qiita.com/terms
Guideline --> http://help.qiita.com/ja/articles/qiita-community-guideline
Privacy --> https://qiita.com/privacy
Help --> https://help.qiita.com
About --> https://qiita.com/about
Users --> /users
Tags --> /tags
Items --> /items
Blog --> https://blog.qiita.com
API --> https://qiita.com/api/v2/docs
Team --> https://teams.qiita.com/
None -->
here --> https://increments.zendesk.com/hc/ja/requests/new

相対パスと絶対パスが混在していますが、
ここではhref属性値が出力できたということで。。

コード解説は・・・

for文の中にhref = a.attrs['href']を追加して、hrefの値を含めて出力しているだけですね。

a_list = soup.findAll("a")
for a in a_list:
    href = a.attrs['href']
    print(a.string, '-->', href)

でも、これでは・・・

href属性値を出力しましたが、これではただ出力しただけになります。
Webスクレイピングで重要なのは、必要な情報のみを抽出することです。

そこで、CSSのセレクタを指定して任意の要素を抽出する方法について説明していきます。

目標は、私のマイページから記事一覧とそのURLを抽出することにしたいと思います。

 

8
5
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
8
5