Morningstar with python requests to get 10 year financial data - python-3.x

It seems morningstar login solution in How can I log in to morningstar.com without using a headless browser such as selenium? is not working anymore for me.
I get error message {'message': 'Forbidden'} and I believe this is because of the login_url used in the solution "https://www.morningstar.com/api/v2/user/login"
Is there any workaround?
import requests
s = requests.session()
auth_url = 'https://sso.morningstar.com/sso/json/msusers/authenticate'
login_url = 'https://www.morningstar.com/api/v2/user/login'
username = 'username'
password = 'password'
headers = {
'Access-Control-Request-Method': 'POST',
'Access-Control-Request-Headers': 'content-type,x-openam-password,x-openam-username',
'Origin': 'https://www.morningstar.com'
}
s.options(auth_url, headers=headers)
headers = {
'Referer': 'https://www.morningstar.com/members/login.html',
'Content-Type': 'application/json',
'X-OpenAM-Username': username,
'X-OpenAM-Password': password,
'Origin': 'https://www.morningstar.com',
}
s.post(auth_url, headers=headers)
data = {"productCode":"DOT_COM","rememberMe":False}
r = s.post(login_url, json=data)
print(s.cookies)
print(r.json())

Related

Run POST API in loop

Import requests
Import json
Headers = {
‘Content-Type’: ‘application/json’
‘Accept’: ‘application/json’,
‘X-AUTH-TOKEN’: ‘API KEY’,
}
data = ‘{“modelId”: “12345”, “cmdbId”: “11122222”}’
Response = requests.post(‘https://splunk/api/modelinfo/, headers=headers, data=data, verify=False)
Above code works fine for single Data.
Now, I have >10 line data in text file as below and need to run the POST API in loop for each line.
modelId: 12345 cmdbId: 11122222
modelId: 12346 cmdbId: 11122223
modelId: 12347 cmdbId: 11122224
import requests
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-AUTH-TOKEN': 'API KEY'
}
payload = [
{'modelId': 12345, 'cmdbId': 11122222},
{'modelId': 12346, 'cmdbId': 11122223},
{'modelId': 12347, 'cmdbId': 11122224}
]
for data in payload:
response = requests.post("https://splunk/api/modelinfo/", headers=headers, data=data, verify=False)
if response.status_code in [200, 201, 204]:
print("post successful")

from docusign sdk i am trying to get envelope but getting "Reason: The URL provided does not resolve to a resource." 404 error

I am trying to create envelope from SDK but getting error like this
Reason: The URL provided does not resolve to a resource.
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache', 'X-DocuSign-TraceToken': '22dad523-9f77-4b83-9481-1e376faf60d8', 'Date': 'Thu, 05 Dec 2019 13:02:26 GMT', 'Content-Length': '0'})
def worker(args):
"""
1. Create the envelope request object
2. Send the envelope
"""
envelope_args = args["envelope_args"]
#print(args)
#print("envelope arrrgggsss")
#print(envelope_args)
# 1. Create the envelope request object
envelope_definition = make_envelope(envelope_args)
#print(envelope_definition)
# 2. call Envelopes::create API method
# Exceptions will be caught by the calling function
api_client = ApiClient()
api_client.host = args["base_path"]
print(api_client.host)
api_client.set_default_header("Authorization", "token" + args["ds_access_token"])
envelopes_api = EnvelopesApi(api_client)
results = envelopes_api.create_envelope(args['account_id'], envelope_definition=envelope_definition)
#print(results)
envelope_id = results.envelope_id
app.logger.info(f"Envelope was created. EnvelopeId {envelope_id}")
return {"envelope_id": envelope_id}
Before that i try to get the token from the following code
def oauth2_token_request(root_url, username, password,
integrator_key):
url = root_url + '/oauth2/token'
data = {
'grant_type': 'password',
'client_id': integrator_key,
'username': username,
'password': password,
'scope': 'api',
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
}
response = requests.post(url, headers=headers, data=data)
print(response)
if response.status_code != 200:
raise exceptions.DocuSignOAuth2Exception(response.json())
return response.json()['access_token']
def oauth2_token_revoke(root_url, token):
url = root_url + '/oauth2/revoke'
data = {
'token': token,
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
}
response = requests.post(url, headers=headers, data=data)
return response
# if response.status_code != 200:
# raise exceptions.DocuSignOAuth2Exception(response.json())
It seems you are hitting wrong API endpoint as
/restapi/v2.1/accounts/9286679/restapi/v2.1/accounts/9286679/envelopes
whereas the correct URI is
/restapi/v2.1/accounts/9286679/envelopes
check your "base_path" value.

Unable to get Python requests with API token

This is my code:
import requests
API key correct
api_key = c87cabcf1b479b807a7b3ba8xxxxxxxxxxxxxx
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Token token=api_key'
}
Getting a List of serves from this API
r = requests.get('https://myserver/api/v1.1/server/', headers = headers, verify=False)
print(r.json())
print (r.status_code)
I am getting the error below. I checked my token correct one and not expires.
r.json() : {'error': {'code': 'error', 'description': 'Authentication credentials were not provided.'}}
r.status_code : 401
I missed somewhere in the header.
I guess problem is may be in your third line of header. Try it like this.
'Authorization': 'Token ' + api_key

Having trouble converting cURL POST command to python requests

I am trying to convert a block of cURL to python requests. I get the following error when I do:
{'error': 'invalid_request', 'error_description': 'request is missing a required parameter or malformed.'}
What am I translating incorrectly?
curl
POST /identity/v1/oauth2/token HTTP/1.1
Host: api.sandbox.ebay.com
Authorization: Basic <B64-encoded-oauth-credentials>
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=<URL-decoded-auth-code>&redirect_uri=<your_redirect_uri>
my_call.py
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
a 'Authorization': f'Basic {<Base64 encoded value>}'
}
data = {
'grant_type': 'authorization_code',
'code': client_id # str,
'redirect_uri': url # str,
'scope': 'https://api.ebay.com/oauth/api_scope/sell.inventory.readonly',
}
def get_oath_token():
url = 'https://api.ebay.com/identity/v1/oauth2/token'
r = requests.post(url, headers=headers, data=data)
print(r.json())

Scraping a page with node.js?

I want to get the flights table for every city from a page but befor that i have to set search fields and hit the button and befor that i have to import captcha at the first page. The web site designed with .Net. I import the captcha by my self but i want to do the rest with nodejs.
This is what i'm doing. first, import the captcha and then from loaded page and by looking at the firebug, set request header and request url and then send using http.request with GET method to scrape the page with nodejs again. after that i get viewstate of the page and again usign firebug, set another request header and "post data" and request url and send them using http.request to scrape the final page. This information that i set using firebug, are fixed; i mean, urls, header options and post data. what i have to change is city name in post data.
But i get a page with empty table. if is possible, What should i do? (sorry for my bad english :) )
This is the url (in persian): http://sepehr.iranhrc.ir.
Thanks in advance.
var http = require('follow-redirects').http;
var querystring = require('querystring');
var cheerio = require('cheerio');
var dataToAttach = {
'ScriptManager1': 'uplFlightSearch|btnSubmit37756070715319',
'__ASYNCPOST': true,
'__EVENTARGUMENT': '',
'__EVENTTARGET': '',
'__LASTFOCUS': '',
'__VIEWSTATE': '',
'__VIEWSTATEGENERATOR': 'E4CF65F9',
'btnSubmit37756070715319': '?????',
'dplFrom': 'THR',
'dplReservationRouteType': 'RoundTrip_FixedDate',
'dplTo': '0',
'dplFlightAdults': '1',
'dplFlightChilds': '0',
'dplFlightInfants': '0',
'txtCountUp': '00:26',
'txtDepartureDate': '1394/04/02',
'txtReturningDate': '1394/04/04'
};
var flightsSearchPageROs = {
hostname: 'sepehr.iranhrc.ir',
path: '/Systems/FA/Reservation/Flight_NewReservation_Search.aspx?qry=sbV7wBdq4B7yEk1yv0OpvmofQkdkbWH49Wjk6UIMGiw95zDjDGo0/ssWJH8Wjv1D',
method: 'GET',
headers:{
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'Cookie': 'ASP.NET_SessionId=2iexj4pfxld4mdilfwttka2q;',
'Content-Type': 'text/html; charset=utf-8',
'Host': 'sepehr.iranhrc.ir',
'Referer': 'sepehr.iranhrc.ir'
}
};
var resultsPageROs = {
hostname: 'sepehr.iranhrc.ir',
path: '/Systems/FA/Reservation/Flight_NewReservation_Search.aspx?action=display&rnd=2378726045210585',
method: 'POST',
headers:{
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'ASP.NET_SessionId=o1ipad335qahuaahc25ngalr;',
'Content-Length': '',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Referer': 'http://sepehr.iranhrc.ir/Systems/FA/Reservation/Flight_NewReservation_Search.aspx',
'Host': 'sepehr.iranhrc.ir',
'Pragma': 'no-cache',
'X-MicrosoftAjax': 'Delta=true',
'X-Requested-With': 'XMLHttpRequest'
}
};
var flightsSearchPage = http.request(flightsSearchPageROs, function(response{
var dataStream = '';
var htmlCode = '';
var date = '';
response.on('data', function(chunk){
dataStream += chunk;
});
response.on('end', function(){
htmlCode = cheerio.load(dataStream);
secondDataToAttach.__VIEWSTATE = htmlCode("__VIEWSTATE").val();
resultsPageROs.headers['Content-Length'] = querystring.stringify(dataToAttach).length;
resultsPageRequest();
});
});
flightsSearchPage.on('error', function(e){console.log("Error0: " + e.message);});
flightsSearchPage.end();
function resultsPageRequest(){
var changingCitiesBoxResponse = http.request(resultsPageROs, function(response){
response.setEncoding('utf8');
var dataStream = '';
var htmlCode = '';
response.on('data', function(chunk){
dataStream += chunk;
});
response.on('end', function(){
htmlCode = cheerio.load(dataStream);
console.log(htmlCode.html());
});
console.log('STATUS: ' + response.statusCode);
});
changingCitiesBoxResponse.on('error', function(e){console.log("Error1: " + e.message);});
changingCitiesBoxResponse.end(querystring.stringify(dataToAttach));
}
EDIT
One thing that i forgot to mention it was that, this is done in PHP with Curl and i looked at the code in curl part and done exactly like that with node and http.request. PHP returns correct answer but mine not.
And I used phantom as well.This is the code:
var url = "http://sepehr.iranhrc.ir/Systems/FA/Reservation/Flight_NewReservation_Search.aspx?action=display&rnd=4565721642440773";
var settings = {
operation: "POST",
encoding: "utf8",
weak: false,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'ASP.NET_SessionId=2iexj4pfxld4mdilfwttka2q;',
'Content-Length': '',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Referer': 'http://sepehr.iranhrc.ir/Systems/FA/Reservation/Flight_NewReservation_Search.aspx?qry=sbV7wBdq4B7yEk1yv0OpvmofQkdkbWH49Wjk6UIMGiw95zDjDGo0/ssWJH8Wjv1D',
'Host': 'sepehr.iranhrc.ir',
'Pragma': 'no-cache',
'X-MicrosoftAjax': 'Delta=true',
'X-Requested-With': 'XMLHttpRequest'
},
data: querystring.stringify(secondDataToAttach)
};
phantom.create(function (ph){
ph.createPage(function (page){
page.open(url2, settings, function(status){
console.log(status);
page.evaluate(function(){ return document.body.innerHTML; }, function(result){
console.log('Content ' + result);
ph.exit();
});
});
});
}, {dnodeOpts: {weak: false}
});
But i didn't get the answer.
And yes, it uses AJAX to send the request. According to Firebug:
The first request is sending a post data and the second one, i think is a redirect that gives the link to the result page(i used that link but no luck) and the third is the result page with the flight details that i can't get.
May be i used them wrong. How can i use these Three request to get flights table?
I think what's happening is that the page you want the results from is loading them through a separate AJAX request, and a regular get request isn't going to get that. You'll need to either figure out what that separate request is and do it yourself, or scrape the page with something like PhantomJS that can execute the javascript for the page.
First though, go to the page on your browser regularly and watch the network tab and locate that request that loads in the data.

Resources