运行代码时报错“TypeError: expected string or bytes-like object”,麻烦老师看下是什么问题
来源:9-6 项目实战之获取cookie并写入配置文件流程重构
qq_LostNFound_0
2019-10-01
#coding=utf-8
import openpyxl
import sys
import os
base_path = os.getcwd()
sys.path.append(base_path)
from Util.handle_ecxel import excel_data
import json
from Util.handle_result import handle_result,handle_result_json,get_result_json
from Util.handle_cookie import get_cookie_value,write_cookie
from Base.base_request import request
class RunMain():
def run_case(self):
rows = excel_data.get_rows()
for i in range(rows-1):
cookie = None
get_cookie = None
data = excel_data.get_rows_value(i+2)
is_run = data[2]
if is_run == 'yes':
method = data[5]
url = data[4]
data1 = data[6]
#print(code)
except_method = data[9]
except_result = data[10]
cookie_method = data[7]
if cookie_method == 'yes':
cookie = get_cookie_value('app')
if cookie_method == 'write':
'''
必须是获取到cookie
'''
get_cookie = {"is_cookie":"app"}
res = request.run_main(method,url,data1,cookie,get_cookie)
#print(res)
code = str(res['errorCode'])
message = res['errorDesc']
if except_method == 'mec':
config_message = handle_result(url,code)
#print("message------",message,"------config_message--->",config_message)
#print(res)
if message == config_message:
excel_data.excel_write_data(i+2,12,"通过")
else:
excel_data.excel_write_data(i+2,12,"失败")
excel_data.excel_write_data(i+2,13,json.dumps(res))
if except_method == 'errorcode':
if except_result == code:
excel_data.excel_write_data(i+2,12,"通过")
else:
excel_data.excel_write_data(i+2,12,"失败")
excel_data.excel_write_data(i+2,13,json.dumps(res))
if except_method == 'json':
if code == 1000:
status_str = 'success'
else:
status_str = 'error'
except_result = get_result_json(url,status_str)
result = handle_result_json(res,except_result)
print(result)
if result:
excel_data.excel_write_data(i+2,12,"通过")
else:
excel_data.excel_write_data(i+2,12,"失败")
excel_data.excel_write_data(i+2,13,json.dumps(res))
#print(data)
if name == “main”:
run = RunMain()
run.run_case()
#coding=utf-8
import sys
import os
import configparser
base_path = os.getcwd()
sys.path.append(base_path)
import requests
import json
from Util.handle_cookie import write_cookie
from Util.handle_init import handle_ini
from Util.handle_json import get_value
class BaseRequest:
def send_post(self,url,data,cookie=None,get_cookie=None):
’’‘
发送post请求
’’‘
response = requests.post(url=url,data=data,cookies=cookie)
if get_cookie != None:
’’’
“is_cookie”:“app”
’’'
cookie_value_jar = response.cookies
cookie_value = requests.utils.dict_from_cookiejar(cookie_value_jar)
write_cookie(cookie_value,get_cookie[‘is_cookie’])
res = response.text
return res
def send_get(self,url,data,cookie=None,get_cookie=None):
'''
发送get请求
'''
response = requests.get(url=url,params=data,cookies=cookie)
if get_cookie != None:
cookie_value_jar = response.cookie
cookie_value = requests.utils.dict_from_cookiejar(cookie_value_jar)
write_cookie(cookie_value,get_cookie['is_cookie'])
res = response.text
return res
def run_main(self,method,url,data,cookie=None,get_cookie=None):
'''
执行方法,传递method、url、data参数
'''
#return get_value(url)
base_url = handle_ini.get_value('host')
if 'http' not in url:
url = base_url+url
print(url)
if method == 'get':
res = self.send_get(url,data,cookie,get_cookie)
else:
res = self.send_post(url,data,cookie,get_cookie)
try:
#res_text = json.loads(res_text)
res = json.loads(res)
#print("----------->",type(res))
except:
print("这个结果是一个text")
return res
request = BaseRequest()
if name == “main”:
request = BaseRequest()
request.run_main(‘get’,‘http://www.imooc.com/login’,"{‘username’:'11111’}")
Traceback (most recent call last):
File “c:/Users/15927/Documents/PYTHONINTERFACE/Run/run_main.py”, line 77, in
run.run_case()
File “c:/Users/15927/Documents/PYTHONINTERFACE/Run/run_main.py”, line 38, in run_case
res = request.run_main(method,url,data1,cookie,get_cookie)
File “C:\Users\15927\Documents\PYTHONINTERFACE\Base\base_request.py”, line 53, in run_main
res = self.send_post(url,data,cookie,get_cookie)
File “C:\Users\15927\Documents\PYTHONINTERFACE\Base\base_request.py”, line 18, in send_post
response = requests.post(url=url,data=data,cookies=cookie)
File “D:\python\lib\site-packages\requests\api.py”, line 116, in post
return request(‘post’, url, data=data, json=json, **kwargs)
File “D:\python\lib\site-packages\requests\api.py”, line 60, in request
return session.request(method=method, url=url, **kwargs)
File “D:\python\lib\site-packages\requests\sessions.py”, line 519, in request
prep = self.prepare_request(req)
File “D:\python\lib\site-packages\requests\sessions.py”, line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File “D:\python\lib\site-packages\requests\models.py”, line 315, in prepare
self.prepare_cookies(cookies)
File “D:\python\lib\site-packages\requests\models.py”, line 572, in prepare_cookies
cookie_header = get_cookie_header(self._cookies, self)
File “D:\python\lib\site-packages\requests\cookies.py”, line 142, in get_cookie_header
jar.add_cookie_header®
File “D:\python\lib\http\cookiejar.py”, line 1345, in add_cookie_header
attrs = self._cookie_attrs(cookies)
File “D:\python\lib\http\cookiejar.py”, line 1304, in _cookie_attrs
self.non_word_re.search(cookie.value) and version > 0):
TypeError: expected string or bytes-like object
1回答
-
TypeError: expected string or bytes-like object
你这里这个错误可以把你这个对象打印出来啊,这里类型不对。或者告诉行号。错误的。
092020-08-06
相似问题