0

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

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'
4

1 回答 1

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))
于 2016-06-23T23:25:36.607 回答