当前位置: 首页 > 工具软件 > indices_view > 使用案例 >

python3 错误string indices must be integers 的解决方法

司寇嘉茂
2023-12-01

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

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 run
    self.get_response_from_page()
  File "/home/python/PycharmProjects/untitled/jd/selenium1.py", line 26, in get_response_from_page
    data = self.get_data_from_response(response)
  File "/home/python/PycharmProjects/untitled/jd/selenium1.py", line 55, in get_data_from_response
    item['价格'] = 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,'&&&&&&&&&&&&&')

 

 

 类似资料: