我用的数据分离将heades的数据放到了excal表中,在执行代码能打印出来json格式的数据,但是post请求时报AttributeError: 'str' object has no attribute 'items'
以下为excal表中的数据:
{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Cookie': 'blogAppAd_blog7article=1; U_TRS1=0000000e.8eeb7a94.5ad22c7f.660e42af; UOR=www.baidu.com,blog.sina.com.cn,; SINAGLOBAL=114.250.89.255_1523723394.617448; closeNotice_fzp=null; rotatecount=2; U_TRS2=0000000e.f5453d79.5ad2bb18.2d1baa02; ULV=1523759833079:3:3:3::1523723612468; Apache=114.250.89.255_1523759897.98579; dlg680=10; sinaads_blogAppAd_blog7article=1',
'Host': 'blog.sina.com.cn',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'}
以下为代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import json
from base import result_need
headers = result_need('Sheet1', 0, 0)
print (headers)
url = 'http://blog.sina.com.cn/s/blog_c59abb380102xbbx.html'
result =requests.get(url,headers=headers)
print (result.text)
以下为执行后报错信息
"D:\PyCharm 2018.1.1\bin\PycharmProjects\untitled\venv\Scripts\python.exe" "D:/PyCharm 2018.1.1/bin/PycharmProjects/untitled/test/search.py"
{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
Traceback (most recent call last):
'Cookie': 'blogAppAd_blog7article=1; U_TRS1=0000000e.8eeb7a94.5ad22c7f.660e42af; UOR=www.baidu.com,blog.sina.com.cn,; SINAGLOBAL=114.250.89.255_1523723394.617448; closeNotice_fzp=null; rotatecount=2; U_TRS2=0000000e.f5453d79.5ad2bb18.2d1baa02; ULV=1523759833079:3:3:3::1523723612468; Apache=114.250.89.255_1523759897.98579; dlg680=10; sinaads_blogAppAd_blog7article=1',
'Host': 'blog.sina.com.cn',
'Upgrade-Insecure-Requests': '1',
File "D:/PyCharm 2018.1.1/bin/PycharmProjects/untitled/test/search.py", line 12, in <module>
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'}
result =requests.get(url,headers=headers)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 494, in request
prep = self.prepare_request(req)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\models.py", line 306, in prepare
self.prepare_headers(headers)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\models.py", line 438, in prepare_headers
for header in headers.items():
AttributeError: 'str' object has no attribute 'items'
Process finished with exit code 1
求大神帮助,感谢
看了以下自己的博客,得到灵感:利用eval()函数解决此问题。在获取excal数据的那一行代码加上eval()函数,如下:headers = eval(result_need('Sheet1', 0, 0))
参考博客地址:http://www.cnblogs.com/yinze/p/8166601.html
因为你读的是字符串,需要转json格式