from urllib.request import Request, urlopen
import json
from config import appcode
host = '
http://wbzy.market.alicloudapi.com'
path = '/rest/160601/text_analysis/attention_text_summary.json'
url = host + path
def attention_text_summary(data):
body1 = "{\"inputs\":[{\"text\":{\"dataType\":50,\"dataValue\":\""
body2 = "\"}}]}"
outputs = json.loads(get_response(body1, data, body2, url, appcode))
dataValue = json.loads(outputs["outputs"][0]["outputValue"]["dataValue"])
return dataValue["summary"]
def get_response(body1, data, body2, url, appcode):
bodys = {}
bodys[''] = body1 + data + body2
post_data = bodys[''].encode('utf8')
request = Request(url, data=post_data)
#request = urllib.request.Request(url, post_data)
request.add_header('Authorization', 'APPCODE ' + appcode)
# 根据 API 的要求,定义相对应的 Content - Type
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urlopen(request)
return response.read().decode("utf-8")
def main():
data = "副省长、省食安委副主任看望慰问一线人感染 H7N9 禽流感防控工作人员。"
dataanys = attention_text_summary(data)
print(type(dataanys))
print(len(dataanys))
print(dataanys)
if __name__ == '__main__':
main()
因为导师的需要,需要调用阿里云的文本分析工具,编程我真是菜的一 B,运行这段代码得到
Traceback (most recent call last):
File "C:/Users/MrvX/Desktop/data_transform - 副本 /task/attention_text_summary.py", line 27, in <module>
main()
File "C:/Users/MrvX/Desktop/data_transform - 副本 /task/attention_text_summary.py", line 20, in main
content = attention_text_summary(data)
File "C:/Users/MrvX/Desktop/data_transform - 副本 /task/attention_text_summary.py", line 14, in attention_text_summary
outputs = json.loads(get_response(body1, data, body2, url, appcode))
File "C:\Users\MrvX\Desktop\data_transform - 副本\get_response.py", line 14, in get_response
response = urlopen(request)
File "C:\Users\MrvX\AppData\Local\Programs\Python\Python36\Lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\MrvX\AppData\Local\Programs\Python\Python36\Lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Users\MrvX\AppData\Local\Programs\Python\Python36\Lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\MrvX\AppData\Local\Programs\Python\Python36\Lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Users\MrvX\AppData\Local\Programs\Python\Python36\Lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\MrvX\AppData\Local\Programs\Python\Python36\Lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
实在看不出哪里有错了,请各位大佬赐教