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
0
440
TUT Python スクレイピングハンズオン
panakuma
February 03, 2018
Tweet
Share
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
420
Other Decks in Education
See All in Education
ブランチ操作 / 02-a-branch
kaityo256
PRO
0
190
The browser strikes back
jonoalderson
0
790
Chapitre_2_-_Partie_3.pdf
bernhardsvt
0
200
GOBUSATA紹介
chankawa919
0
130
MySmartSTEAM 2526
cbtlibrary
0
210
コマンドラインの使い方 / 01-d-cli
kaityo256
PRO
0
100
Gitの仕組みと用語 / 01-b-term
kaityo256
PRO
0
190
Introduction - Lecture 1 - Next Generation User Interfaces (4018166FNR)
signer
PRO
2
4.5k
Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)
signer
PRO
0
3k
Surviving the surfaceless web
jonoalderson
0
730
【dip】「なりたい自分」に近づくための、「自分と向き合う」小さな振り返り
dip_tech
PRO
0
260
AIで日本はどう進化する? 〜キミが生きる2035年の地図〜
behomazn
0
130
Featured
See All Featured
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
160
The agentic SEO stack - context over prompts
schlessera
0
690
Java REST API Framework Comparison - PWX 2021
mraible
34
9.2k
Utilizing Notion as your number one productivity tool
mfonobong
4
260
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
470
Everyday Curiosity
cassininazir
0
160
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
70
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.1k
KATA
mclloyd
PRO
35
15k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
200
Amusing Abliteration
ianozsvald
0
130
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.2k
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の中身を出力