我想把爬取的内容一个循环在json文件写一行,我自己写的程序老是把每个循环爬取到的内容以很多行的形式存储到json文件中,大家有什么建议吗?
import requests
headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Referer': 'http://www.wikipedia.org/',
'Connection': 'keep-alive',
}
fileObject = open('gongshi.json', 'w', encoding='utf-8')
for i in range(10):
response = requests.get('https://pubchem.ncbi.nlm.nih.gov/sdq/sdqagent.cgi?infmt=json&outfmt=json&query={%22select%22:%22*%22,'
'%22collection%22:%22compound%22,%22where%22:{%22ands%22:[{%22*%22:%22CCCC%22}]},'
'%22order%22:[%22relevancescore,desc%22],%22start%22:'+str(i)+'1,%22limit%22:10,%22width%22:1000000,'
'%22listids%22:0}', headers=headers)
fileObject.write(response.text+'\n')
fileObject.close()