我是 Python 新手。
我想知道如何为多个网址运行以下代码的相同过程。
# Code '1,完美运行
url ='https://toyama.com.br/wp-json/wp/v2/assistencia?local=914&ramo=&_embed&per_page=100'
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
df = pd.read_json(url)
resp = requests.get(url, headers=header)
pandas_data_frame1 = df['acf'].apply(pd.Series)
pandas_data_frame1.to_csv ('teste2.CSV', encoding ='utf-8-sig')
# Code2,工作不完美(多个网址,重要的是要注意有些网址存在而其他网址不存在,我需要处理这种结构)
url1 =['https://toyama.com.br/wp-json/wp/v2/assistencia?local=914&ramo=&_embed&per_page=100',
'https://toyama.com.br/wp-json/wp/v2/assistencia?local=800&ramo=&_embed&per_page=100',
'https://toyama.com.br/wp-json/wp/v2/assistencia?local=933&ramo=&_embed&per_page=100',
'https://toyama.com.br/wp-json/wp/v2/assistencia?local=844&ramo=&_embed&per_page=100',
'https://toyama.com.br/wp-json/wp/v2/assistencia?local=806&ramo=&_embed&per_page=100',
'https://toyama.com.br/wp-json/wp/v2/assistencia?local=1207&ramo=&_embed&per_page=100']
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
for links in url1:
df = pd.read_json(links)
resp1 = requests.get(links, headers=header)
data = json.loads(resp1.text)
for d in data:
pandas_data_frame1 = df['acf'].apply(pd.Series)
pandas_data_frame1.to_csv ('teste2.CSV', encoding ='utf-8-sig')
#不幸的是只保存了链接'https://toyama.com.br/wp-json/wp/v2/assistencia?local=1207&ramo=&_embed&per_page=100' >
我需要的是有一个 csv,其中我将 json 键作为一列,就像代码 1 一样。
亲切的问候!