码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-01-06 19:23:08      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:方案   字典   use   形参   爬虫   strong   时报   解决方案   sts   

我在使用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' 错误解决方案

标签:方案   字典   use   形参   爬虫   strong   时报   解决方案   sts   

原文地址:https://www.cnblogs.com/yoyowin/p/12157800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!