简体   繁体   中英

How do I convert Python Code to JavaScript

I was trying to convert my code from Python to JS, because I want to use HTML with my JavaScript code. My question is, how do I convert this code from Python to JavaScript without parsing Python. Sorry for the messy wording.


#Python Code


import requests

import json

import datetime

  url = https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=jpmf&interval=60min&apikey=testkey


  response = requests.get(url)

  assert response.status_code == 200

  data =json.loads(response.text)


Without using any frameworks:

const https = require('https');

https.get('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=jpmf&interval=60min&apikey=testkey', (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });

}).on('error', (e) => {
  console.error(e);
});

Just see node documents

GitHub Copilot can convert Python to JavaScript on your behalf:

/*
import requests

import json

import datetime

url = https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=jpmf&interval=60min&apikey=testkey


response = requests.get(url)

assert response.status_code == 200

data =json.loads(response.text)
*/

// create a function that does the above python code
function doStuff() {
    let url = 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=jpmf&interval=60min&apikey=testkey';
    let response = fetch(url);
    let data = response.json();
    return data;
}

Note that doStuff will return a promise of the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM