IBM Bluemix Watson Alchemy如何引用笔记本电脑上的本地目录

2024-05-12 21:27:10 发布

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

目前,我正在使用以下代码对一个网站进行分析:

import json
from os.path import join, dirname
from watson_developer_cloud import AlchemyLanguageV1

alchemy_language = AlchemyLanguageV1(api_key='YOUR API KEY')

url = 'https://developer.ibm.com/watson/blog/2015/11/03/price-reduction-
for-watson-personality-insights/'

combined_operations = ['page-image', 'entity', 'keyword', 'title', 
'author', 'taxonomy', 'concept', 'doc-emotion'] 
print(json.dumps(alchemy_language.combined(url=url, 
extract=combined_operations), indent=2))

有人能告诉我如何引用本地目录,在那里我有自己的html文件进行分析吗?我尝试使用以下代码,但它不起作用:

#html ='C:\Users\Downloads\Python\name8.htm'

Tags: 代码fromimportjsonurldeveloperos网站
1条回答
网友
1楼 · 发布于 2024-05-12 21:27:10

使用html时,需要提供一个字符串变量,其中包含要分析的HTML代码。在代码中,您尝试使用文件路径作为内容。显然,这不是HTML代码。你知道吗

尝试使用:

import json
from os.path import join, dirname
from watson_developer_cloud import AlchemyLanguageV1

alchemy_language = AlchemyLanguageV1(api_key='YOUR API KEY')

combined_operations = ['page-image', 'entity', 'keyword', 'title',
                       'author', 'taxonomy', 'concept', 'doc-emotion']

with open('C:\Users\Downloads\Python\name8.htm', 'rb') as html:
    print(json.dumps(alchemy_language.combined(html=html.read(),
          extract=combined_operations), indent=2))

相关问题 更多 >