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

简介: '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)


相关文章
|
1月前
|
计算机视觉 Python
解决 NoneType‘ object has no attribute ‘astype’ 问题
解决 NoneType‘ object has no attribute ‘astype’ 问题
42 0
|
7月前
|
存储 JSON 数据格式
数据集加载时报错'dict' object has no attribute 'requests‘
数据集加载时报错'dict' object has no attribute 'requests‘
134 5
|
1月前
AttributeError: 'NoneType' object has no attribute 'to_capabilities'
AttributeError: 'NoneType' object has no attribute 'to_capabilities'
398 0
|
1月前
|
机器学习/深度学习 监控 数据可视化
【已解决】 ‘Conv2d’ object has no attribute ‘register_full_backward_hook’
【已解决】 ‘Conv2d’ object has no attribute ‘register_full_backward_hook’
|
1月前
|
安全 计算机视觉 Python
【已解决】attributeerror: ‘FreeTypeFont‘ object has no attribute ‘getsize‘
【已解决】attributeerror: ‘FreeTypeFont‘ object has no attribute ‘getsize‘
|
8月前
|
Python Windows
xlrd库报错【AttributeError: ‘ElementTree‘ object has no attribute ‘getiterator‘】
xlrd库报错【AttributeError: ‘ElementTree‘ object has no attribute ‘getiterator‘】
142 0
|
1月前
AttributeError ‘NoneType‘ object has no attribute ‘to_capabilities‘
AttributeError ‘NoneType‘ object has no attribute ‘to_capabilities‘
137 0
|
1月前
|
JSON 数据格式 Python
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
107 1
|
1月前
|
数据采集
运行爬虫时报错AttributeError—— 'str' object has no attribute 'capabilities'
运行爬虫时报错AttributeError—— 'str' object has no attribute 'capabilities'
383 0
|
1月前
'WebDriver' object has no attribute 'find_element_by_tag_name'
'WebDriver' object has no attribute 'find_element_by_tag_name'
162 0