0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

今日のスクレイピングの復習

Last updated at Posted at 2019-03-04

Lv.0 大前提

①class = ○○(HTML)→.○○ { }(CSS) え?↓

<div class = "search">外国人の彼氏</div>

「外国人の彼氏」を装飾したい。

classをつけて適当に"search"と名前をつける。

.search{
  文字の大きさ変えたり
 背景色変えたり
}

class部分は
.search{ }と書けば、
CSS(装飾)があたる。
(そういうものです。)

②inner_textって何?

<p>季節のコーヒー</p>

「季節のコーヒー」部分がinner_text
「p」の部分はinner_textではない。

Lv.1 検証画面→コピペ←私は何をやっていたの?

contents = documents.css(".searchResult_itemTitle")
         ①     ②  ③

画面を指差し確認しながらがいいかもです。

①ドキュメント(文章)の中の
②CSSで、(Lv.0)

③class名が
 "searchResult_itemTitle"
 (検索結果の項目のタイトル)
 となっている文章を
④「contents」という箱(変数)に入れてあげる。
 ※contentsという名前は作った人が勝手に命名した。
  englishでもSeasonCoffeeでも何でもいい。

Lv.2 each文の登場

eachからendまでが1つの塊です。

contents.each do |content|
①    ②   ③  
  puts content.inner_text
       ⑤    ⑥
end

----------1行目----------------------------
①contentsという箱(変数)に入った文章を
②まず1個箱から…
③出して…
④「content」という箱に入れてください。
※このcontentも勝手に命名。
----------2行目-----------------------------
⑤「content」に入った文章の
⑥inner_textの部分をputsしてください。(Lv.0)
----------end------------------------------
⑦putsしたら、
 contetsの箱の中身が無くなるまで
 ①から⑥を繰り返してください。

上と同じコードです見づらかったらどうぞ
contents.each do |content|
①    ②   ③  
  puts content.inner_text
       ⑤    ⑥
end

Lv.3 これ学ぶ意味あるの? 社会のどこで使うの?

すごい使えます。すごい使ってます。

例:Twitt○r
みんなのツイートを一覧で見る例の画面

みんなのtweet○.each do |twe○t|
  puts twe○t
end

他にもF○cebookやi○stagramでも
このように実装されています。

SNSの根幹をなしています。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?