菜鸟求问一个最最基本的 Python request 问题

2017-12-23 11:44:45 +08:00
 mrvx
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

实在看不出哪里有错了,请各位大佬赐教
2558 次点击
所在节点    全球工单系统
4 条回复
Trim21
2017-12-23 12:15:08 +08:00
为什么不用 requests 要用 urllib 呢…
zbl430
2017-12-23 17:33:17 +08:00
code 请用 markdown 语法, 你这样很难看, 自己写吧,用楼上说的,代码会简单很多很多(这代码水平不高)
scusjs
2017-12-23 18:11:13 +08:00
mrvx
2017-12-23 20:03:17 +08:00
问题解决。。按一楼大佬的办法重新用了 requests 发现新世界
阿里云的示例是用 urllib 写的。。真是绕了很大的弯子


# -*- coding: UTF-8 -*-
import requests
import json


host = ''
path = ''
url = host+path
appcode = ""
body = {
"inputs": [
{
"text": {
"dataType": 50,
"dataValue": "“像家一样”,这样的酒店宣传语估计要大打折扣了,理由就是——脏!日前,加拿大一项堪称最大规模的调查显示,包括速 8、喜来登以及假日酒店在内的 6 大连锁酒店用品,如床上用品、遥控器等都有细菌,威胁住客健康,希望连锁酒店给住客一个真正干净的“家”"
}
}
]
}
headers = {
'Authorization':'APPCODE ' +appcode,
'Content-Type':'application/json; charset=UTF-8'
}
r = requests.post(url, data=json.dumps(body), headers=headers)
text = json.loads(r.text)
print(type(text))

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/417020

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX