Python - 通过循环URL列表进行API调用

时间:2017-12-18 11:05:44

标签: python python-3.x

我试图使用Alchemy API为URL列表提取关键字/实体 - 但是作为Python的相对新手 - 我不确定如何执行此操作以及众多Stack Overflow文章我已经读过了,没有给我任何结果。

因此,是否有人知道如何在列表或单独的txt / csv文件中对URL列表进行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))

1 个答案:

答案 0 :(得分:0)

您可以从文本文件中逐行阅读

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

只需循环阅读文本文件即可。