2017-12-18 132 views
-1

我试图使用Alchemy API为URL列表提取关键字/实体 - 但是作为Python的相对新手 - 我不知道该怎么做这和我读过的无数堆栈溢出文章并没有让我产生任何结果。Python - 通过循环访问URL列表进行API调用

因此,有没有人知道我可以在列表中或在单独的txt/csv文件中对API列表进行API调用?

import json 
from watson_developer_cloud import NaturalLanguageUnderstandingV1 
from watson_developer_cloud.natural_language_understanding_v1 \ 
import Features, EntitiesOptions, KeywordsOptions 

natural_language_understanding = NaturalLanguageUnderstandingV1(
    username='Username', 
    password='Password', 
    version='2017-02-27') 

response = natural_language_understanding.analyze(
url=('https://www.example.com/'), 
    features=Features(
    entities=EntitiesOptions(
     emotion=True, 
     sentiment=True, 
     limit=2), 
    keywords=KeywordsOptions(
     emotion=True, 
     sentiment=True, 
     limit=2))) 

print(json.dumps(response, indent=2)) 
+2

你有什么试过,没有奏效?提示:“API调用”部分在这里完全不相关 - 问题是“我如何将单个项目的函数应用于列表(或任何其他迭代)的项目” –

+0

nb:http://idownvotedbecau.se/noattempt/ –

回答

0

您可以从一个文本文件

myFile = open("myText.txt", "r") 
URL = myFile.readline() 

就遍历从文本文件中读取他们读他们一行行。

+0

谢谢福斯特!我现在唯一的问题是,它给我一个状态400错误信息。所以我不确定这是否意味着我已经实现了错误的(最有可能的)错误的建议,或者是否需要为此循环添加一个睡眠,以便IBM Watson不会吓坏了? –