LoginSignup
1
1

More than 5 years have passed since last update.

WatirでFacebookの仕様を監視(取得)する

Last updated at Posted at 2013-10-21

やりたいこと

  • Facebookの仕様が変わったら知りたい
  • 以前の仕様がどんなだったかを見れるようにしておきたい

技術課題

  • Facebookの仕様が書いてあるdevelopersには、ブラウザからでないとアクセスできない => watir使う
    • wgetしたらブラウザインストールを促されるページが返ってきた
  • 裏側で勝手にやってくれるようにしたい => headless使う
  • proxyを越える必要がある => Firefox AddonのAutoAuth使う

処理概要

  • 取得するページURLはconfig/url_list.txtに書いておく
  • ブラウザはfirefoxを使う
    • AutoAuth読み込みは別記事に記載予定
  • 取得したページの保存
  • ページはspecification/ 以下に日付でディレクトリを作成し、その下にパスに応じたディレクトリ・名前で保存する
    • / -> developers.html
    • /docs/ -> developers/docs.html
    • /docs/facebook-login/ -> developers/docs/facebook-login.html
    • /blog/archive -> developers/blog/archive.html

コード

facebook_developers.rb
require 'watir-webdriver'
require 'headless'
require 'yaml'
require 'uri'

# Facebook User
facebook_user = YAML.load_file("config/facebook_user.yml")

#URL List Setting
url_list = []
File.open("config/url_list.txt") do |f|
  f.each do |line|
    url_list << line.chomp
  end
end

#Firefox Setting
headless = Headless.new
headless.start
config = YAML.load_file("config/firefox.yml")
profile = Selenium::WebDriver::Firefox::Profile.from_name(config["profile"])
profile.add_extension config["extension_path"]
begin
  browser = Watir::Browser.new(:firefox, :profile => profile)
rescue Selenium::WebDriver::Error::WebDriverError => e
  puts "Please unset system proxy setting"
end

begin
  browser.goto "https://www.facebook.com/"
  browser.text_field(:name, "email").set(facebook_user["email"])
  browser.text_field(:name, "pass").set(facebook_user["password"])
  browser.input(:value => "ログイン").click

  url_list.each do |url|
    date = Time.now.to_s.split.first.gsub("-", "")
    dir_path = "./specification/#{date}"
    uri_path = URI.parse(url).path
    if uri_path == "/"
      file_name = "developers.html"
      dir_name  = dir_path
    else
      ary = uri_path.split("/")
      file_name = ary.last + ".html"
      ary.pop
      dir_name  = dir_path + ary.join("/")
    end

    browser.goto url
    FileUtils.mkdir_p dir_name unless Dir.exist? dir_name
    File.open(File.join(dir_name, file_name), "w") do |file|
      file.write(browser.html)
    end
  end

ensure
  browser.close
  headless.destroy
end
1
1
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
1
1