Skip to content

Instantly share code, notes, and snippets.

@RaymondChou
Created August 23, 2013 06:47
Show Gist options
  • Save RaymondChou/6316235 to your computer and use it in GitHub Desktop.
Save RaymondChou/6316235 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'nokogiri'
http = Net::HTTP.new('trade.jd.com', 80)
# 登录后的cookie
cookie = 'aview=843.783520|843.791660|843.771412|877.674903|688.813736|877.674899|688.823463|688.768334; atw=688.813736.14|843.783520.11|877.674903.4; pin=4077039-235606; npin=4077039-235606; logintype=qq; unick=freezestart; ceshi3.com=A64E8CE1156ECFCB00EC559A187D88322AB429A3EFAD42082ED22DD481E4F1AA3BAED95977D278EF2735A6F543D424395E486E7E719225E15665BF29C4C51D760E33948B1C2D3C6B3A8B98081AD52E451E58C4E95F7C9ECDCEB8F819FB683131C337CEE1F37760EDF0D9A574157844D653335B21A7EA51E2C7281CADDBD302C2E9AC84770278B1940620C238809C8FAB92D2E45AD0C562F20C35F58E55742274; eb=0; reWids4=1015970968; vpflag=\"\"; cn=1; _jzqco=%7C%7C%7C%7C1371459554633%7C1.417866817.1367743079141.1371459876094.1371460190862.1371459876094.1371460190862.0.0.0.25.25; ipLoc-djd=12-904-3379; ipLocation=%u6c5f%u82cf; track=30275297-8610-4032-8bef-c1fc7b1c6d71; __jda=122270672.133917276.1364047666.1368619380.1371459558.7; __jdb=122270672.5.133917276|7.1371459558; __jdc=122270672; __jdv=122270672|www.jd.com|-|referral|-'
headers = {
'Cookie' => cookie,
'Referer' => 'http://trade.jd.com',
'Content-Type' => 'application/x-www-form-urlencoded'
}
# 省
path = '/dynamic/consignee/getProvinces.action'
data = ''
resp, _ = http.post(path, data, headers)
resp = resp.body.force_encoding('GBK').encode('UTF-8')
province_array ||= []
doc = Nokogiri::HTML(resp)
fh = File.new("province.txt", "w")
fh.puts 'province_id|province_name'
doc.css('option').each_with_index do |option,i|
next if i == 0
province = option.content.gsub('*','')
province_id = option['value']
province_array << province_id
fh.puts "#{province_id}|#{province}"
end
fh.close
# 市
path = '/dynamic/consignee/getCitys.action'
city_array ||= []
fh = File.new("city.txt", "w")
fh.puts 'city_id|province_id|city_name'
province_array.each do |province_id|
data = "consigneeParam.provinceId=#{province_id}"
resp, _ = http.post(path, data, headers)
resp = resp.body.force_encoding('GBK').encode('UTF-8')
doc = Nokogiri::HTML(resp)
doc.css('option').each_with_index do |option,i|
next if i == 0
city = option.content.gsub('*','')
city_id = option['value']
city_array << city_id
fh.puts "#{city_id}|#{province_id}|#{city}"
end
end
fh.close
# 区
path = '/dynamic/consignee/getCountys.action'
county_array ||= []
fh = File.new("county.txt", "w")
fh.puts 'county_id|city_id|county_name'
city_array.each do |city_id|
data = "consigneeParam.cityId=#{city_id}"
resp, _ = http.post(path, data, headers)
resp = resp.body.force_encoding('GBK').encode('UTF-8')
doc = Nokogiri::HTML(resp)
doc.css('option').each_with_index do |option,i|
next if i == 0
county = option.content.gsub('*','')
county_id = option['value']
county_array << county_id
fh.puts "#{county_id}|#{city_id}|#{county}"
end
end
fh.close
# 街道
path = '/dynamic/consignee/getTowns.action'
fh = File.new("town.txt", "w")
fh.puts 'town_id|county_id|town_name'
county_array.each do |county_id|
data = "consigneeParam.countyId=#{county_id}"
resp, _ = http.post(path, data, headers)
resp = resp.body.force_encoding('GBK').encode('UTF-8')
doc = Nokogiri::HTML(resp)
doc.css('option').each_with_index do |option,i|
next if i == 0
town = option.content.gsub('*','')
town_id = option['value']
fh.puts "#{town_id}|#{county_id}|#{town}"
end
end
fh.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment