这个错误意思是字符串的下标一定要是整数
出这种错误有多种可能,最形象直接的就是:

a = '[abc]'
print(a['0'])

有点pyhton基础的都知道下标怎么能是字符串’0’,必须是整数0,1,2,3…

a = '[abc]'
print(a[0])

才是正确的

第二种是json格式导致的错误:

#部分代码
skuid = re.findall('https://item.jd.com/(\d+).html',response.url)[0]
price_url = 'https://p.3.cn/prices/mgets?skuIds=J_{}'.format(skuid)
print(price_url,'7777777777777')
res = requests.get(price_url)
print(res.text,'**********')
print(item,'&&&&&&&&&&&&&')

报错如下:

https://p.3.cn/prices/mgets?skuIds=J_100000822981 7777777777777
[{"op":"4499.00","m":"9999.00","id":"J_100000822981","p":"4499.00"}]
**********
Traceback (most recent call last):File "/home/python/PycharmProjects/untitled/jd/selenium1.py", line 80, in <module>jsp.run()File "/home/python/PycharmProjects/untitled/jd/selenium1.py", line 72, in runself.get_response_from_page()File "/home/python/PycharmProjects/untitled/jd/selenium1.py", line 26, in get_response_from_pagedata = self.get_data_from_response(response)File "/home/python/PycharmProjects/untitled/jd/selenium1.py", line 55, in get_data_from_responseitem['价格'] = res.text[0]['p']
TypeError: string indices must be integers

因为获取价格url的response(res)是个json数据,所以要json.loads(),才能把json格式转为python识别的格式。
正解:

skuid = re.findall('https://item.jd.com/(\d+).html',response.url)[0]
price_url = 'https://p.3.cn/prices/mgets?skuIds=J_{}'.format(skuid)
print(price_url,'7777777777777')
res = requests.get(price_url)
print(res.text,'**********')
item['价格'] = json.loads(res.text)[0]['p']
print(item,'&&&&&&&&&&&&&')

python3 错误string indices must be integers 的解决方法相关推荐

  1. Python错误:TypeError: string indices must be integers

    在项目遇到一个错误 : TypeError: string indices must be integers 由于是返回的接口数据,所以一时也分辨不出是哪里的错,然后我就还原了这样的场景: unico ...

  2. Python 错误:TypeError String Indices Must be Integers 【已解决】

    Python 错误:TypeError String Indices Must be Integers [已解决] 如果你尝试使用字符串值,而不是整数值来访问字典或可迭代对象的值,那么你将收到以下错误 ...

  3. string indices must be integers错误原因

    string indices must be integers:字符串索引必须是整数 一.使用字符串为索引 a = [1, 2, 3, 4] print(a['0']) '0'为字符串,0才是整形.这 ...

  4. 解决TypeError: string indices must be integers, not str

    遇到问题 ExtendValue = {"area": "1","info": "{\"year\": 201 ...

  5. TypeError: string indices must be integers

    1.错误描述 >>> print(st[-1,-6]); Traceback (most recent call last):File "<pyshell#13> ...

  6. python之解析 yaml配置文件时TypeError: string indices must be integers

    #定义一个SuperLogin登录函数,传递3个形参数 def SuperLogin(lANIP,username, password):driver = webdriver.Chrome()driv ...

  7. python string indices must be_Python解析json时提示“string indices must be integers”问题解决方法...

    本文实例讲述了Python解析json时提示"string indices must be integers"问题解决方法.分享给大家供大家参考,具体如下: import json ...

  8. python string indices must be_python报错string indices must be integers,怎么解决?

    展开全部 python报错string indices must be integers解决如下: 在.py文件中写的data={"a":"123"," ...

  9. sqlserver 遇到以零作除数错误的处理 不报错的解决方法

    sqlserver 遇到以零作除数错误的处理 不报错的解决方法 参考文章: (1)sqlserver 遇到以零作除数错误的处理 不报错的解决方法 (2)https://www.cnblogs.com/ ...

最新文章

  1. C# 学习笔记(14)自己的串口助手----多行发送
  2. 基于WOA-SVM算法的乳腺肿瘤识别算法的MATLAB仿真
  3. Oracle 11g中创建实例
  4. BATJ面试必会|Jvm 虚拟机篇
  5. 详解linux下安装python3环境
  6. 响应式布局技术:App如何适配不同尺寸的设备
  7. 2010_8_18_静态路由_静态路由路由排错(不连续地址汇总)
  8. 自动生成mybatis代码
  9. cadence 介绍
  10. Filter过滤器:使用过滤器保护指定资源,只有登录用户才能访问。若访问内容时用户没有登录则跳转到登录页面。
  11. GitBook入门(用github做出第一本书)——超详细配图说明
  12. 自建微信公众号文章搜索舆情系统
  13. 在网页上面录制声音(chrome、firefox浏览器可用)
  14. 基于OpenCV实现简单人脸面具、眼镜、胡须、鼻子特效(详细步骤 + 源码)
  15. ROS学习(开篇)Ubuntu16.04安装ROS Kinetic详细教程
  16. 百度大脑3月新品推荐:EasyDL视频目标追踪全新发布
  17. Personal views on domain change of several theorems and applications
  18. 少儿知识付费做好内容是关键
  19. 动态canvas 相册简单效果展示
  20. 安德玛知识产权案再次胜诉;和铂医药完成1.028亿美元C轮融资 | 美通企业日报...

热门文章

  1. Python Module_subprocess_调用 Powershell
  2. Ubuntu安装FTP 服务, 实现和Windows的文件互传
  3. SecureCRT自动保存窗口数据到文件,自动保存日志,每行添加时间戳
  4. 电感和磁珠的区别及应用场合和作用
  5. 机器学习入门-决策树算法
  6. O11ycon会议讨论了可观察性的收益和挑战
  7. 最新 react 开发框架(webpack2、react15、react-router4、antd-mobile)
  8. 【转】python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode...
  9. libtool的工作原理
  10. 2018-01-02 JavaScript实现ZLOGO: 用语法树实现多层循环...