作者Shenk (闲客)
看板Ruby
标题Re: [问题] 关於网页资料撷取~~~
时间Sat Jun 11 00:38:21 2011
之前有写过一个把网页作成command line可以存取的程式
可以给你参考
require 'open-uri'
require 'password'
require 'hpricot'
require 'net/http'
url = ARGV[0]
begin
cookie=open('cookie').read
file = open(url, 'Cookie'=>cookie)
doc = Hpricot.parse(file)
throw Exception.new if (doc/'tr[@valign="top"] > td[@align="right"]
>a[@href="/login/"]').length != 0
rescue Exception => e
uri = URI.parse('
http://some.web.site.com/')
req = Net::HTTP::Post.new('/login/')
warn "Password?"
password = Password.get
csrf = Net::HTTP.new(uri.host, uri.port).start {|http| http.get('/login/')}
csrfmiddlewaretoken =
(Hpricot.parse(csrf.body)/'input[@name="csrfmiddlewaretoken"]').attr('value')
req.add_field 'Refer','
http://some.web.site.com/login/'
req.add_field 'Cookie', csrf['set-cookie']
req.form_data = {
:email=>'
[email protected]', :password =>password,
:csrfmiddlewaretoken => csrfmiddlewaretoken,
:next => '/'
}
result= Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req)}
puts result
open('cookie','w'){ |f| f.puts result['set-cookie']}
cookie=result['set-cookie']
file = open(url, 'Cookie'=>cookie)
doc = Hpricot.parse(file)
end
※ 引述《Terence223 (水镜)》之铭言:
: 最近在做一个东西因此需要撷取特定网页的资讯
: 目前已知如何直接用ruby撷取原始码并用regular express去分析
: 再取得自己所需要的资料字串
: 但是这个网页
: http://www.tdcc.com.tw/smWeb/QryStock.jsp
: 需要输入资讯才会冒出我所要的资料 ex : 证券代号填1101
: 而且网址也无任何变动
: 请问一下要怎麽使用ruby来取得资料填完後的网页原始码?
: 麻烦指点一下 谢谢^^"
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.114.232.77