'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)


相关文章
|
3月前
|
TensorFlow 算法框架/工具 Python
【Tensorflow 2】解决'Tensor' object has no attribute 'numpy'
解决'Tensor' object has no attribute 'numpy'
62 3
|
3月前
|
API C++ Python
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
|
4月前
|
并行计算 Serverless API
函数计算操作报错合集之出现 "AttributeError: 'NoneType' object has no attribute 'pop'" 错误,是什么原因
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
149 1
|
4月前
|
数据处理 API 索引
【Python】已解决:AttributeError: ‘Series‘ object has no attribute ‘sortlevel‘
【Python】已解决:AttributeError: ‘Series‘ object has no attribute ‘sortlevel‘
184 4
|
3月前
|
TensorFlow API 算法框架/工具
【Tensorflow+keras】解决使用model.load_weights时报错 ‘str‘ object has no attribute ‘decode‘
python 3.6,Tensorflow 2.0,在使用Tensorflow 的keras API,加载权重模型时,报错’str’ object has no attribute ‘decode’
53 0
|
4月前
|
Python
【Python】已解决:(Python xlwt写入Excel报错)AttributeError: ‘function’ object has no attribute ‘font’
【Python】已解决:(Python xlwt写入Excel报错)AttributeError: ‘function’ object has no attribute ‘font’
106 0
|
4月前
|
Python
【Python】已解决:(Python写入Excel表格报错)‘NoneType’ object has no attribute ‘write’
【Python】已解决:(Python写入Excel表格报错)‘NoneType’ object has no attribute ‘write’
241 0
|
4月前
|
XML 数据格式 Python
【Python】已解决:AttributeError: ‘function’ object has no attribute ‘ELement’
【Python】已解决:AttributeError: ‘function’ object has no attribute ‘ELement’
157 0
|
4月前
|
API Python
【Python】已解决:AttributeError: ‘TfidfVectorizer’ object has no attribute ‘get_feature_names_out’
【Python】已解决:AttributeError: ‘TfidfVectorizer’ object has no attribute ‘get_feature_names_out’
118 0
|
4月前
|
开发者 Python
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
119 0

热门文章

最新文章