gny*_*his 14 google-api google-finance
我注意到Google删除了Google App Engine的Finance API.我想要的只是他们在Google财经投资组合中的股票代码清单.有没有办法从最终用户的产品组合中提取这些数据,因为已经删除了API?我正在尝试手动检索它,因为我知道登录名和密码(例如,它是我自己的).
有没有办法通过curl手动检索它,登录到Google服务?看起来应该可以登录并转到我的投资组合页面,检索源代码.
我试过以下代码:
#!/bin/bash
function ClientLogin() {
read -p 'Email> ' email
read -p 'Password> ' -s password
local service=$1
curl -s -d Email=$email -d Passwd=$password -d service=$service https://www.google.com/accounts/ClientLogin | tr ' ' \n | grep Auth= | sed -e 's/Auth=//'
}
function GetFinance() {
curl -L -s -H "Authorization: GoogleLogin auth=$(ClientLogin finance)" "http://www.google.com/finance/portfolio?action=view&pid=1" &> output.html
}
GetFinance
Run Code Online (Sandbox Code Playgroud)
但是,此代码仅检索告诉我登录的页面.解决方案不需要使用curl,但它必须是使用某种脚本语言的自动检索.
感谢x4avier,我了解了casperjs,并且能够编写一个快速脚本来加载Google服务登录页面,输入用户名和密码,然后获取Google财经投资组合.我相信这适用于任何其他谷歌服务和页面.我将投资组合的html保存到portfolio.html.希望这也有助于其他人.
var fs = require('fs');
var failed = [];
var links = [
"https://www.google.com/finance/portfolio?action=view&pid=13"
];
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: false, // The WebPage instance used by Casper will
loadPlugins: false, // use these settings
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537
}
});
// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
var url = 'https://accounts.google.com/ServiceLogin?service=finance';
casper.start(url, function() {
// search for 'casperjs' from google form
console.log("page loaded");
this.test.assertExists('form#gaia_loginform', 'form is found');
this.fill('form#gaia_loginform', {
Email: 'youraccount@gmail.com',
Passwd: 'yourpass'
}, true);
});
casper.each(links, function(casper, link) {
this.then(function() {
this.test.comment("Loading " + link);
start = new Date();
this.open(link);
});
this.then(function() {
var message = this.requestUrl + " loaded";
if (failed.indexOf(this.requestUrl) === -1) {
this.test.pass(message);
fs.write('portfolio.html',this.getPageContent(),'w');
}
});
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
您应该考虑使用像casper.js这样的无头浏览器.
有了它,您可以登录谷歌,转到谷歌财务,并获取页面或特定的CSS选择器的HTML.
要登录,您将使用fill()函数,它的工作方式如下:
casper.start('http://admin.domain.tld/login/', function() {
this.fill('form[id="login-form"]', {
'username': 'chuck',
'password': 'n0rr1s'
}, true);
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
然后你可以用getHTML()解析页面和具体内容,工作如下:
casper.then(function() {
this.echo(this.getHTML('h1#foobar')); // => 'The text included in the <h1 id=foobar>'
});
Run Code Online (Sandbox Code Playgroud)
CasperJs使用cookie并浏览多个页面,它应该符合您的需求.
希望能帮助到你 :)
归档时间: |
|
查看次数: |
4839 次 |
最近记录: |