拉取百度汉语的成语接龙脚本
2016-07-22 22:13
656 查看
成语不存在或期望的接龙长度超出可查询范围时会报错,不过凑合能找到部分接龙的成语,记录之。
#encoding:GBK
require 'net/http'
require 'nokogiri'
def get_alist(url)
puts url
$stdout.flush
res = Net::HTTP.get_response(URI.parse(url))
doc = Nokogiri::HTML res.body
return doc.xpath("//*[@id='jielong-wrapper']")[0].css('div')[0].css('a')
end
def build_long(ci,long,maxlen)
url = "http://hanyu.baidu.com/s?wd=#{URI.escape(ci.encode('utf-8'))}&cf=jielong&ptype=idiom"
alist = get_alist(url)
i = 1
alist.each do |a|
if long.has_key?(a.text) then
return build_long(a.text,long,maxlen)
else
long[a.text] = 0
return long if long.size > maxlen
return build_long(a.text,long,maxlen) if alist.size <= i
end
i = i + 1
end
end
start_chengyu = '尔虞我诈'
max_length = 20
long = Hash.new
long = build_long(start_chengyu,long,max_length - 1)
result_file = File.open('成语接龙结果文件.txt','w')
long.each do |k,v|
result_file.puts k
end
#encoding:GBK
require 'net/http'
require 'nokogiri'
def get_alist(url)
puts url
$stdout.flush
res = Net::HTTP.get_response(URI.parse(url))
doc = Nokogiri::HTML res.body
return doc.xpath("//*[@id='jielong-wrapper']")[0].css('div')[0].css('a')
end
def build_long(ci,long,maxlen)
url = "http://hanyu.baidu.com/s?wd=#{URI.escape(ci.encode('utf-8'))}&cf=jielong&ptype=idiom"
alist = get_alist(url)
i = 1
alist.each do |a|
if long.has_key?(a.text) then
return build_long(a.text,long,maxlen)
else
long[a.text] = 0
return long if long.size > maxlen
return build_long(a.text,long,maxlen) if alist.size <= i
end
i = i + 1
end
end
start_chengyu = '尔虞我诈'
max_length = 20
long = Hash.new
long = build_long(start_chengyu,long,max_length - 1)
result_file = File.open('成语接龙结果文件.txt','w')
long.each do |k,v|
result_file.puts k
end
相关文章推荐
- LeetCode - 208. Implement Trie (Prefix Tree)
- JAVA 中 Arrays.sort 中的排序的方法
- Post、Get方法拿到的是字符串
- bzoj2728理解了好久
- Gym 100114C (找循环节)
- Ubuntu 修改时区
- Python之MySql操作
- requireJS 用法
- HDU 5723(最小生成树+dfs)
- HDU 1241 DFS算法
- Update now: Macs and iPhones have a Stagefright-style bug!
- [LeetCode刷题笔记]Math数学类型题目(三)特殊的数字结构
- 欢迎使用CSDN-markdown编辑器
- Android学习笔记18:自定义Seekbar拖动条式样
- 数论·欧拉函数
- 【POJ3667】Hotel-线段树
- 24点计算器Ruby脚本
- moive纹理(待补充)
- 3. php 环境
- Filter visualization