如何通过快递发送API响应

时间:2019-07-14 05:43:15

标签: javascript node.js express localhost

我目前正在尝试通过我的newsapi.org调用传递JSON结果。但是我不知道该怎么做?任何帮助将是巨大的!谢谢

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

### Read Data
df = pd.read_csv('data.csv')

### Prepare X, Y & ln(Y)
X = df.sort_values(by=['x']).loc[:, 'x':'x']
Y = df.sort_values(by=['x']).loc[:, 'y':'y']
ln_Y = np.log(Y)

### Use the relation ln(Y) = ln(A) - BX to fit X to ln(Y)
from sklearn.linear_model import LinearRegression
exp_reg = LinearRegression()
exp_reg.fit(X, ln_Y)
#### You can introduce weights as well to apply more bias to the smaller X values, 
#### I am transforming X arbitrarily to apply higher arbitrary weights to smaller X values
exp_reg_weighted = LinearRegression()
exp_reg_weighted.fit(X, ln_Y, sample_weight=np.array(1/((X - 100).values**2)).reshape(-1))

### Get predicted values of Y
Y_pred = np.exp(exp_reg.predict(X))
Y_pred_weighted = np.exp(exp_reg_weighted.predict(X))

### Plot
plt.scatter(X, Y)
plt.plot(X, Y_pred, label='Default')
plt.plot(X, Y_pred_weighted, label='Weighted')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()

plt.show()

1 个答案:

答案 0 :(得分:2)

如果您希望在每个新请求中调用API,则可以将其放入请求处理程序中:

    import UIKit
    import WebKit

    class ViewController: UIViewController, WKNavigationDelegate {
        var webView: WKWebView!
        var progressView: UIProgressView!

        var websites = ["apple.com", "hackingwithswift.com"]

        override func loadView() {
            webView = WKWebView()
            webView.navigationDelegate = self
            view = webView
        }

        override func viewDidLoad() {
            super.viewDidLoad()

    //        let url = Bundle.main.url(forResource: "publish", withExtension: "html", subdirectory: "website")!

//------Correction--------\\\

            let url = URL(string: "http://" + websites[1])! /// you can use index 0 / 1 according to site you wanna show in Web view \\\

//------Correction--------\\\

    //        webView.loadFileURL(url, allowingReadAccessTo: url)
            webView.load(URLRequest(url: url))
            webView.allowsBackForwardNavigationGestures = true

    ......

如果您希望每隔一段时间调用一次API并缓存结果,则可以执行以下操作:

app.get('/', function (req, res){
    newsapi.v2.topHeadlines({
      category: 'general',
      language: 'en',
      country: 'au'
    }).then(response => {
      //console.log(response);
      res.send(response);
    }).catch(err => {
      res.sendStatus(500);
    });
});