试图在任务计划程序中运行python脚本,但由于request.urlopen(url)而中断

2024-09-28 21:59:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我的脚本在spyder中工作,但当我尝试在任务调度(使用bat文件)中运行它时,它不会执行任何操作。我相信这是URL请求出现问题的原因,我可能需要使用URL解析器。。。但我不确定这对我的代码会起什么作用。(API密钥不是我的,而是测试员)

import urllib.request as request
import json
import pandas as pd

ETFS = ["VTI"]    

def Name (ticker):

    url = "https://eodhistoricaldata.com/api/fundamentals/{ticker}.US?api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX".format(ticker=ticker)

    with request.urlopen(url) as response:

            source = response.read()
            data = json.loads(source)          

    type(data.get('General',{}).get('Name', []))
    len(data.get('General',{}).get('Name', []))
    Name = (data.get('General',{}).get('Name', []))
    return (Name)

resultname = list(map(Name, ETFS))

d = { "Ticker": ETFS, 'Name': resultname}
dfx = pd.DataFrame(data=d)
dfx.to_excel("Equity ETFs.xlsx")  


Tags: nameimportapijsonurldatagetresponse