我正在尝试从报废的数据中制作一个 json 文件。然而,根据我的函数 converToJson() 它不断覆盖前一个条目而不是附加。是因为我没有迭代它吗?例如:下面的 Json 文件每次都会用新数据覆盖第一个条目,而不是附加到它。
[{"Volume": "Volume:\n6,061,086", "Price": "$41.88", "Name": "Suncor Energy Inc."}]
def getStockDetails(url, browser):
print(url)
browser.get(url)
quote_wrapper = browser.find_element_by_css_selector('div.quote-wrapper')
quote_name = quote_wrapper.find_element_by_class_name(
"quote-name").find_element_by_tag_name('h2').text
quote_price = quote_wrapper.find_element_by_class_name("quote-price").text
quote_volume = quote_wrapper.find_element_by_class_name(
"quote-volume").text
print("\n")
print("Quote Name: " + quote_name)
print("Quote Price: " + quote_price)
print("Quote Volume: " + quote_volume)
print("\n")
convertToJson(quote_name,quote_price,quote_volume)
def convertToJson(quote_name,quote_price,quote_volume):
quotesArr = []
quoteObject = {
"Name": quote_name,
"Price": quote_price,
"Volume": quote_volume
}
quotesArr.append(quoteObject)
with open('trendingQuoteData.json', 'w') as outfile:
json.dump(quotesArr, outfile)
元芳怎么了
慕尼黑5688855
相关分类