So I got problem that I can't merge two dictionaries into one dictionary. This is my code:
def create_app():
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
news_site = request.get_json()
print(news_site)
scrapdata = {}
scrapdata2 = {}
if news_site.get('kompas', True) and news_site.get('detik', True) == True:
scrapdata = kompas_fun()
scrapdata2 = detik_fun()
fscrapdata = {**scrapdata, **scrapdata2}
return jsonify(fscrapdata)
elif news_site.get('kompas', True) == False:
scrapdata = detik_fun()
fscrapdata = {**scrapdata}
return jsonify(fscrapdata)
elif news_site.get('detik', True) == False:
scrapdata = kompas_fun()
fscrapdata = {**scrapdata}
return jsonify(fscrapdata)
else:
return jsonify({'value': 'error'})
return app
When input matched with if
condition, it's just return with scrapdata2
dictionary only instead merge both scrapdata
and scrapdata2
. So it's just scrapdata2
dictionary as the result
I already tried solution from How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?, but still not work. Or use reduce
, update
and ChainMap
but still not work.
Edit:For input I'm using Postman using json, this is the input:
{
"kompas":true,
"detik":true
}
This is the output from my code above, but I only post few from many dictionary from the output:
{
"data": [
{
"author": "Sachril Agustin Berutu",
"category": "detikNews",
"content": "Beredar dokumen hasil tes swab Habib Rizieq Shihab (HRS) positif Corona. Wali Kota Bogor Bima Arya mengaku tidak mengetahui apakah dokumen tes swab Habib Rizieq itu asli atau palsu., \"Kita tidak bisa pastikan itu asli apa palsu,\" kata Bima saat dihubungi, Rabu (2/12/2020)., \n",
"date": "02 Des 2020",
"headline": "\nDialog 212 Dimulai, Habib Rizieq Hadir di Lokasi Terpisah\n",
"link": "https://news.detik.com/berita/d-5277968/dialog-212-dimulai-habib-rizieq-hadir-di-lokasi-terpisah",
"tag": "habib rizieq",
"time": "09:58 WIB",
"total comment": "240"
},
{
"author": "Achmad Dwi Afriyadi",
"category": "detikFinance",
"content": "Industri hulu migas merupakan industri yang penuh ketidakpastian. Untuk menarik investasi, pemerintah berupaya mengurangi ketidakpastian tersebut., Menteri ESDM Arifin Tasrif mengatakan, ketidakpastian sendiri berasal eksternal dan internal.",
"date": "02 Des 2020",
"headline": "\nCara Pemerintah 'Manjakan' Investor Migas\n",
"link": "https://finance.detik.com/energi/d-5278096/cara-pemerintah-manjakan-investor-migas",
"tag": "migas",
"time": "11:18 WIB",
"total comment": "0"
}
],
"news_portal": "detik"
}
Both kompas_fun()
and detik_fun()
returning a dictionary from web scraping from two news website. But the output only from detik_fun()
. The kompas_fun()
is works and returning a list but not merged with result from detik_fun
.
This is end from kompas_fun()
which appending and converting result to dictionary:
arti.append({
'headline': title,
'content':content,
'writer': writer,
'editor': editor,
'category' : cat,
'tag' : tag1,
'total comment': comment,
'date': dates,
'time': times,
'read count': rcount,
'link': lnk
})
df = pd.DataFrame(arti)
list_dct = df.to_dict(orient='records')
dct = {"date": scrapdate, 'news_portal': 'kompas', "data": list_dct}
return dct
From detik_fun()
is same like above. The different is just `'news_portal': 'detik'.
Any help would be appreciated.