When parsing the site, the result2 and result3 are sometimes missing and only result1 is recorded, is it possible to add a condition on the existence of these elements and if there are no them to fill them in?

for row in table.find_all('tr')[1:]: cols = row.find_all('td') projects.append({ 'result1': cols[1].span.text, 'result2': cols[2].span.text, 'result3': cols[3].span.text }) 
  • What in your projects and what it means there are no result2 and result3 ? 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). - Klimenkomud
  • In general, it is better to cycle through your cols - Klimenkomud
  • Damn litter did not write, 'projects' is a list. In the list I throw a dictionary with the names 'result1' and 'result2' and the values ​​from the parsing 'cols'. ie, the site can be either all three of these values ​​or only one. I want to add a condition when 'cols [2] .span.text' and 'cols [3] .span.text' are not found then assign a value to 'result2'. - santos_q
  • result1 , result2 , result3 - bad names. Instead of a dictionary, you can simply pass a tuple: project.append(('первая колонка', 'вторая', 'третья')) - jfs

3 answers 3

You can add columns with a default value if they are missing:

 NCOLS = 3 cols = [span.text for span in row.select('td span')] # CSS selector cols += ['default'] * (NCOLS - len(cols)) # pad to NCOLS if necessary projects.append(dict(zip(['result'+str(i) for i in range(1, NCOLS+1)], cols))) 

Related question: to perform a series of calculations when intermediate results may be missing, see Maybe "kind-of" monad in python .

  • Well, I will try. - santos_q

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.

     for row in table.find_all('tr')[1:]: cols = row.find_all('td') projects.append({ 'result1': cols[1].span.text if cols[1].span.text else "default", 'result2': cols[2].span.text if cols[2].span.text else "default", 'result3': cols[3].span.text if cols[3].span.text else "default" }) 
    • Thanks for the decision) - santos_q