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

时间:2016-06-23 11:27:32

标签: python ibm-cloud ibm-watson personality-insights

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

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'

1 个答案:

答案 0 :(得分:0)

使用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))