Python通过循环访问url列表来进行API调用

2024-04-25 04:28:21 发布

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

我正在尝试使用Alchemy API为URL列表提取关键字/实体-但是作为Python的相对新手-我不知道如何做到这一点,我读过的大量堆栈溢出文章也没有给我带来任何结果。你知道吗

因此,有人知道我如何对列表或单独的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))

Tags: fromimportapijsontruecloudurldeveloper