Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
TUT Python スクレイピングハンズオン
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
panakuma
February 03, 2018
Education
450
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
TUT Python スクレイピングハンズオン
panakuma
February 03, 2018
More Decks by panakuma
See All by panakuma
jsnog-lt-1_イベントNOCの裏側
panakuma
0
280
DTCP-IPをVPNで
panakuma
0
3.1k
TUT Python 初心者入門
panakuma
0
440
Other Decks in Education
See All in Education
Visionary Initiative: Materials-Positive Society — Evolving “Things,” empowering a positive society | Science Tokyo
sciencetokyo
PRO
0
130
2026年度春学期 統計学 第9回 確からしさを記述する ー 確率 (2026. 5. 28)
akiraasano
PRO
0
140
0513
cbtlibrary
0
220
Gitがない時代 インターネットがない時代の 開発話
sapi_kawahara
0
320
2026年度春学期 統計学 第3回 クロス集計と感度・特異度,データの可視化 (2026. 4. 23)
akiraasano
PRO
0
200
The Art & Science of Elearning
tmiket
1
240
[2026前期火5] 論理学(京都大学文学部 前期 第12回)「証明を走らせる:カリー・ハワード対応」
yatabe
0
190
自己紹介 / who-am-i
yasulab
6
7k
[2026前期火5] 論理学(京都大学文学部 前期 第10回)「論理学の哲学——意味とは何か(Tonkと推論主義)」
yatabe
0
190
[2026前期火5] 論理学(京都大学文学部 前期 第6回)「かつとまたはの規則」
yatabe
0
430
AI-Based Speaking Assessment of a Short-Term Study Abroad Program
uranoken
0
370
2026年度春学期 統計学 第7回 データの関係を知る(2)ー 回帰と決定係数 (2026. 5. 21)
akiraasano
PRO
0
200
Featured
See All Featured
Done Done
chrislema
186
16k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
190
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
260
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
180
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
240
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Designing for Performance
lara
611
70k
A Tale of Four Properties
chriscoyier
163
24k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
380
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Transcript
Python Boot Camp in TUT Python 初心者入門 第2回 スクレイピング
スクレイピングとは • ウェブサイトから情報を抽出するコンピュータソフトウェア技術のこと。 (Wikipedia 日本語版 ウェブスクレイピング より引用)
スクレイピングに必要なモノ(ライブラリ) •urlilb5 •beautifulsoup4
ライブラリのインストール (1) •pipyというライブラリ管理ツールを使います。 •ubuntu でいうaptitude(apt)と同じような感じのものと思って下さい。
ライブラリのインストール (2) •まず、pip3をaptでインストールします。 •sudo apt install python3-pip •pip3でurlli5とbeautifulsoup4をインストールします。 •sudo pip3
install urllib5 beautifulsoup4
ライブラリの使い方 (1) •プログラム内でライブラリを使うときにはimportをします。 • importの仕方 ライブラリ全体をimport import ライブラリ名 ライブラリの一部をimport from
ライブラリ名 import 関数名など
ライブラリの使い方 (2) •importしたものに別の名前をつけることもできます。 •例えば長い関数名を省略したいときなんかに便利です。 from ライブラリ名 import 関数名 as 別名
スクレイピングの基本 •まずスクレイピングするサイトの構造を観察します。 •自分が取得したい情報が入っているタグなどを見つけます。 •またタグに振ってあるクラスやIDも手がかりになります。
構造の観察 (1) •今回、「妹さえいればいい。」のニュースページをスクレイピングして いきたいと思いますので、まずそのサイトを開き、キーボードの[F12] を押して下さい。 •開発者ツールが開きますので、開発者ツール左上の要素選択ツー ルをクリックして、拾いたい要素(今回はニュースのタイトル)をクリッ クします。
構造の観察 (2) •クリックすると、HTMLソースの対応した部分をブラウザが表示してく れます。
構造の観察 (3) •この要素は h1タグで 「c-thumb-index__title」というクラスである •ということがわかります。 •これを手がかりに、データを抽出していきます。
スクレイピング (1) •まずは以下のサイトのコードを「scraping.py」というファイルに入力し て実行してみたください。 https://goo.gl/mDyxXU
None
スクレイピング (2) • 行っていることの解説 • まず最初の2行 from urllib import request
from bs4 import BeautifulSoup as BS • urllibというライブラリからrequestという機能をインポート • bs4というライブラリからBeautifulSoupという機能をインポートしてBSという別 名を付与
スクレイピング (3) url = "http://imotosae.com/news/" req = request.Request(url) res =
request.urlopen(req) html = res.read() • 変数urlにスクレイピングするサイトのurlを代入 • urlを取得するというオブジェクトをreqに代入 • reqを実行して得られたオブジェクトをresに代入 • htmlにresをStringとして代入
スクレイピング (4) soup = BS(html, "lxml") topics = soup.find_all('h1', 'c-thumb-index__title')
for i in range(len(topics)): print(topics[i].string, "\n") • htmlを「lxml」というHTMLパーサを使って内容を解析して 結果を変数 soup に代入 • soupの中から「c-thumb-index__title」というクラス名を持つ「h1」タ グを抽出して変数 topics にリストとして代入 • topicsの中身を出力