Python爬取加拿大Circle K门店时出现TypeError:string indices must be integers报错
错误原因
- 缩进逻辑错误:翻页
for循环只包含了url生成的代码,接口请求、数据解析、CSV写入的逻辑都在循环外,程序只会执行最后一页的请求,无法爬取全量数据 - 数据类型不匹配:报错提示你正在用字符串的键索引取值,说明某页接口返回的
stores['stores']不是预期的字典结构,或遍历到的单条store是字符串而非字典,大概率是爬取过程中接口返回了异常响应(比如触发反爬返回了错误提示字符串)
修复参考代码
import requests import json import csv import time # 先创建CSV写入表头 with open('circleK12.csv', mode='w', encoding='utf-8', newline='') as CSVFile: writer = csv.writer(CSVFile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL) # 可根据需求增加经纬度、国家等字段 writer.writerow(["address", "city", "latitude", "longitude", "country"]) headers = { 'Connection': 'keep-alive', 'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"', 'Accept': '*/*', 'X-Requested-With': 'XMLHttpRequest', 'sec-ch-ua-mobile': '?0', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Dest': 'empty', 'Referer': 'https://www.circlek.com/store-locator?address=Toronto,Ontario,Canada&lat=43.6529&lng=-79.3849', 'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8', 'dnt': '1' } x = range(0,955) for pages in x: # 页面请求相关逻辑缩进放到for循环内部 url = f"https://www.circlek.com/stores_new.php?lat=49.2609&lng=-123.1139&services=®ion=global&page={pages}" payload={} try: response = requests.request("GET", url, headers=headers, data=payload, timeout=10) stores = json.loads(response.text) # 判断返回结构是否符合预期,异常页直接跳过 if not isinstance(stores, dict) or 'stores' not in stores or not isinstance(stores['stores'], dict): print(f"第{pages}页返回结构异常,跳过") time.sleep(2) continue # 追加写入CSV with open('circleK12.csv', mode='a', encoding='utf-8', newline='') as CSVFile: writer = csv.writer(CSVFile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL) for store in stores['stores'].values(): # 过滤非字典格式的异常门店数据 if not isinstance(store, dict): continue # 只保留加拿大门店可把下面注释放开 # if store.get("country", "") != "CA": # continue Match_address = store.get("address", "") Match_city = store.get("city", "") Match_lat = store.get("latitude", "") Match_lng = store.get("longitude", "") Match_country = store.get("country", "") writer.writerow([Match_address, Match_city, Match_lat, Match_lng, Match_country]) print(f"第{pages}页爬取完成") # 加延迟避免触发反爬 time.sleep(1.5) except Exception as e: print(f"第{pages}页爬取出错:{str(e)},跳过") time.sleep(3) continue
额外说明
生成的CSV文件可以直接用Excel打开,如果你只需要加拿大的门店,把代码中过滤国家的注释放开即可,自动过滤其他国家的点位。
内容的提问来源于stack exchange,提问作者John_Muir
相关产品推荐
相关产品推荐

