Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created August 18, 2015 08:46
Show Gist options
  • Save kimoto/b75f400465309b3f8c1e to your computer and use it in GitHub Desktop.
Save kimoto/b75f400465309b3f8c1e to your computer and use it in GitHub Desktop.
商標検索をもとに読みを取得する
#!/bin/env ruby
# encoding: utf-8
require 'mechanize'
def syouhyou_yomi(*keywords)
keywords.flatten!
wait_time = 0.2
agent = Mechanize.new
agent.user_agent_alias = 'Mac Mozilla'
#agent.log = Logger.new(STDERR)
results = {}
keywords.each{ |keyword|
# キーワード内のスペースは除去します
keyword.gsub!(" ", "")
top_url = "https://www.j-platpat.inpit.go.jp/web/all/top/BTmTopPage"
page = agent.get(top_url)
#sess_id = page.search("#jplatpatSession").first.text
page = page.form_with(:name => "searchForm") do |form|
form.action = '/web/all/top/BTmTopSearchPage.action'
sess_id = page.search("#jplatpatSession").first.text
form.field_with(:name => 'bTmFCOMDTO.searchKeyword').value = keyword
form.field_with(:name => 'bTmFCOMDTO.searchValue').value = "4"
form.field_with(:name => 'bTmFCOMDTO.enzanShiValue').value = "1"
form.add_field!('jplatpatSession', sess_id)
end.submit()
result_count = page.search(".searchbox-result-count").text.gsub("件", "").to_i
STDERR.puts "(#{keyword}) result count: #{result_count}"
STDERR.puts page.body
if result_count == 0
# not found yomi
sleep wait_time
next
end
page = page.form_with(:name => 'syouhyouLink') do |form|
end.submit()
tr = page.search(".table-result > tbody > tr").first
url = tr.search("td > a").first.attributes["href"].value
page = agent.get(url)
#yomi = page.search("/html/body/div/div[6]/div/div[2]/div[1]/table[2]/tr[3]/td").text
yomi = nil
page.search(".table-information > tr").each{ |tr|
yomi_index = nil
tr.search("th").each_with_index{ |th, index|
if th.text == "(561)称呼(参考情報)"
yomi_index = index
end
}
if yomi_index
yomi = tr.search("td")[yomi_index].text
break;
end
}
# 複数候補がある場合は, で区切られている
yomis = yomi.split(",")
results[keyword] = yomis
sleep wait_time
}
results
end
#p syouhyou_yomi(["BeachBunny", "mabitex", "stodja", "KSUBI"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment