開発環境 Ubuntu 16.04.6 LTS/Ruby 2.6.3
状況
現在rubyでweb上の天気情報をスクレイピングして出力するプログラムを作成中です。
nokogiriを使って情報を取得するところまではできたのですが、cssでデータ抽出する段階で詰まっています。
require "open-uri" require "nokogiri" url = 'https://tenki.jp/forecast/3/16/4410/13101/' charset = nil html = open(url) do |f| charset = f.charset f.read end doc = Nokogiri::HTML.parse(html,nil,charset) doc.css(".today-weather").each do |node| day = node.text.slice(/\d月\d+日/) end puts day
上のコードを実行すると
raceback (most recent call last): scrape_yahoo_title.rb:17:in <main>': undefined local variable or method day for main:Object (NameError)
となりました。
該当部分のhtmlファイルが
<section class="today-weather"><!-- 今日の天気 --> <h3 class="left-style">今日 07月17日<span class="weekday">``` 上記であったので、パースされたhtmlファイルであるdocの中でも today-weatherタグ以下をnodeと置き直し、日付以外の情報を抽出する際の cssの記述量を減らそうという考えでした。 どの時点で考え方を間違えているのかがあやふやです。ご指摘をお願いいたします。 参考にしたサイト http://ec2-54-248-216-128.ap-northeast-1.compute.amazonaws.com/blog/2012/getting-started-with-nokogiri/ https://qiita.com/yamotech/items/56dc132f7e6c5050098d
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/17 14:55