めうの雑記

備忘録

rubyでニュースを取得する

require 'open-uri'
require 'nokogiri'

def getInfo(url)
    
    charset = nil
    
    html = open(url) do |f|
        charset = f.charset # 文字種別を取得
        f.read # htmlを読み込んで変数htmlに渡す
    end
    
    doc = Nokogiri::HTML.parse(html, nil, charset)
    
    doc.xpath("//li[@class='hasImg']").each do |ele|
        p ele.css("h3").text
        p ele.css("a")[0][:href]
    end

end

category_list = ["dom", "world", "eco"]
for category in category_list
    url = "http://news.livedoor.com/topics/category/" + category + "/?p="
    for i in 1..2
        p i.to_s + "ページ目"
        getInfo(url + i.to_s)
    end
end

f:id:umesann:20190227215432p:plain:w450