You can do the following:
cols = row.find_all('td') result1 = cols[0].span.text result2 = cols[1].span.text result3 = cols[2].span.text if result2 and result3: projects.append({ 'result1': result1, 'result2': result2, 'result3': result3 }) elif result2: projects.append({ 'result1': result1, 'result2': result2, 'result3': 'some data' }) elif result3: projects.append({ 'result1': result1, 'result2': 'some another data', 'result3': result3 }) else: projects.append({ 'result1': result1, 'result2': 'some another data', 'result3': 'some data' })
This is not a very good solution, but in your case it will fit. In general, it is better to cycle through your cols , and where you are processing the results, do not rely on the mandatory presence of keys in this dictionary.
projectsand what it means there are noresult2andresult3? There are no values, or do you also have such variables? If there are simply no values, then it is possible. In general, please add more code regarding this place. And yes, in python, indexation does not begin with unity, but with 0 (this is me, just in case). - Klimenkomudcols- Klimenkomudresult1,result2,result3- bad names. Instead of a dictionary, you can simply pass a tuple:project.append(('первая колонка', 'вторая', 'третья'))- jfs