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)
issueis a dictionary that cannot be accessed outside of thegotHolidays()method. Or maybe it is just a formatting problem on StackOverflow?