'str' object has no attribute 'get' 错误解决方案

Posted yoyowin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了'str' object has no attribute 'get' 错误解决方案相关的知识,希望对你有一定的参考价值。

我在使用python写爬虫时用到了requests.get()方法:

def openUrl(url, ip, agent):
    #函数形参为url:网页地址; ip:ip池; agent:User-Agent, 三者均为字符串类型
    requests.get(url, headers=agent, proxies=ip)

疑惑的是,使用时报了 ‘str’ object has no attribute ‘get’ 错误

查看文档后发现,是由于get()方法中的headers和proxies参数应传入字典而不是字符串,于是经修改,代码成功运行:

def openUrl(url, ip, agent):
    headers = {User-Agent: agent}
    proxies = {http : ip}
    requests.get(url, headers=headers, proxies=proxies)

以上是关于'str' object has no attribute 'get' 错误解决方案的主要内容,如果未能解决你的问题,请参考以下文章

解决编码问题:AttributeError: 'str' object has no attribute 'decode'

AttributeError: 'str' object has no attribute 'decode'

为啥Python会写str' object has no attribute 'andswith?

AttributeError: ‘str‘ object has no attribute ‘decode‘

'str' object has no attribute 'get' 错误解决方案

django-AttributeError: 'str' object has no attribute 'decode'