Python 3.7 AttributeError: 'str' object has no attribute 'items' 报错怎么解决

图片说明
求大佬帮忙看下这样的报错怎么解决

items()方法是字典的用法,对于str是没有该用法的,从报错的地方看,说明你的header是string类型,而非字典类型,找到对应的位置,看看你想得到什么结果:

>>>
>>> k={'as':1,'fds':2}
>>> k.items()
dict_items([('as', 1), ('fds', 2)])
>>>
>>> j='qwfguqf'
>>> j.items()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'items'

文件---------------ua_info-----------如下
import random

ua_list = [
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
'User-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11',
'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0',
' Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1',
' Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
]
a = random.choice(ua_list)
print(a)

main 文件如下---------------------
from urllib import request
import ua_info
url = ' 404 Not Found http://httpbin.org/get'
headers = ua_info.a

print(headers)

headers = {

'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:65.0) Gecko/20100101 Firefox/65.0'}

1、创建请求对象,包装ua信息

req = request.Request(url=url, headers=headers)
res = request.urlopen(req)
html = res.read().decode('utf-8')
print(html)

问问题至少带上出错部分源代码,不然鬼知道你怎么搞出来的错,除非是出错可能性本身就很小又很多人出错的问题,出错位置是你requests请求那句,不是你url格式的问题就是你header格式s的问题

import requests

url = "https://ask.csdn.net/questions/747787"

headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'}

data = requests.get(url,headers = headers)

print(data)

我这段代码执行反正是成功返回200、

我怎么知道你哪里出错的