本帖最后由 YunGuo 于 2021-2-5 02:23 编辑
很多处错误,爬虫写代码要细心。
把你代码改了一下后运行没问题。import requests
lis1 = []
lis2 = []
lis3 = []
url = 'https://cn-digital2-app.bmw.com.cn/dlo/v1/locales?brand_id=1'
headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36'}
r = requests.get(url, headers=headers)
r_json = r.json()['data']
for i in r_json:
p_name = i['name_cn']
p_code = i['id']
lis1.append(p_code)
value_city = i['cities']
for j in value_city:
c_name = j['name_cn']
c_code = j['id']
lis2.append([c_name, c_code])
for code in lis1:
url_1 = 'https://cn-digital2-app.bmw.com.cn/dlo/v1/outlets'
params = {'brand_id': 1, 'province_id': code}
r_2 = requests.get(url_1, headers=headers, params=params)
r_2json = r_2.json()['data']
for item in r_2json:
dealer_shortname = item['shortname_cn']
dealer_name = item['name_cn']
dealer_addr = item['addr_cn']
dealer_tel = item['tel']
lis3.append([dealer_name, dealer_shortname, dealer_addr, dealer_tel])
|