I'm using Python for the first time to run a crawler. I've got the crawler to work and now I want to save the results in my MongoDB using pymongo,but for some reason I get this error: "NameError: name 'city' is not defined". If I wrap city in quotes,it works as expected,but I want to save it as it is. Anyone has done something similar or know what the solution is?
def gotHolidays(self, response):
cityName = response.meta['name']
feriado = []
facult = []
for selector in response.css("span.one"):
feriado.append(selector.css("::text").extract())
for selector in response.css("span.two"):
facult.append(selector.css("::text").extract())
city = {
'city': cityName,
'holidays':{
'facult': facult,
'feriado': feriado
}
}
print(json.dumps(city))
from pymongo import MongoClient
client = MongoClient()
client = MongoClient ('localhost', 27017)
db = client['myBank']
myCollection = db.myCollection
myCollection_data = {
'cities': city
}
result = myCollection.insert_one (myCollection_data)
Copyright Notice:Content Author:「Bruce Espeschit」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51442558/insert-document-in-mongodb-using-pymongo