python接口请求post raw格式
目的:根据提供的服务接口,将要处理的数据以raw格式请求该服务接口,接收该接口返回的处理信息,并写入新的excel文件中
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/6/2 14:58
# @Author : ctt
# @Software: PyCharm
import pandas as pd
import requests
import json
def post_data(title, content):
url = 'http://192.168.1.149:7000/doc_event/br/pred'
data = json.dumps({'title': title,'content': content})
headers = {
'Content-Type': 'application/json',
'Cookie': 'sessionid=eyJ1aWQiOiI5NDZhMGNjOC02NTU4LTQ0OGYtOGQ3ZC1jMmFjMGY5ODg4YjciLCJvcmdhbml6YXRpb25fcGsiOjF9:1loK5m:PgOoqBZBdQPCJrgFr7O7RdOkV1HOq2VmOZx5lAoD-CI'
}
response = requests.post(url, data, headers=headers)
return str(response.text)
df = pd.read_excel('中资企业项目.xlsx', usecols=['标题', '内容'])
contents = df['内容'].tolist()
titles = df['标题'].tolist()
out_df = pd.DataFrame(columns=['标题','内容','项目名称','国家','业主单位', '承建单位', '工期', '项目阶段', '项目金额'])
for (index, title) in enumerate(titles):
print(index)
print(title)
result = post_data(title=title,
content=contents[index])
result = json.loads(result)
print(result)
print(type(result))
if result['handleMsg'] == 'success':
project_name = result['resultData']['project_name']
project_state = result['resultData']['state']
project_A = result['resultData']['company_identification_Party_A']
project_B = result['resultData']['company_identification_Party_B']
project_cycle = result['resultData']['project_cycle']
project_status = result['resultData']['project_status']
project_money = result['resultData']['amount_of_cooperation']
out_df.loc[index] = [title, contents[index], ','.join(project_name), ','.join(project_state),
','.join(project_A), ','.join(project_B),
','.join(project_cycle), ','.join(project_status),
','.join(project_money)]
print(out_df)
out_df.to_excel('中资企业项目处理.xlsx', index=False)