PepperoniPizza
PepperoniPizza

Reputation: 9112

Shorter try except block for many vars

I am parsing some content out of different urls. Not all the urls have the same structure and thus the code fails for some urls, so the code I came up with is this (simplified version):

    meta_dict = {}
    try:
        meta_dict['date_published'] = html.find('date'}).text
    except:
        meta_dict['date_published'] = ''
    try:
        meta_dict['headline'] = html.find('headline').text
    except:
        meta_dict['headline']
    try:
        meta_dict['description'] = html.find('description').text
    except:
        meta_dict['description']

    return meta_dict

This being a simplified block, but the idea is to try and get more than 50 variables and doing a try except block for every one of them just feels too repetitive and ugly in the code too.

I know I could make a function for it and return '' if it fails, but I want to know if there is another way to handle this case.

Upvotes: 0

Views: 44

Answers (2)

Alex
Alex

Reputation: 19124

l = [('date_published', 'date'), ('headline', 'headline'), ('description', 'description')]
for dict_val, html_val in l:
    try:
        meta_dict[dict_val] = html.find(html_val).text
    except:
        meta_dict[dict_val] = ''

Upvotes: 2

Eugene S
Eugene S

Reputation: 6910

If the list of these variables you are checking is constant, you can put them into a list and then just iterate over that list.

vars = [date_publis, hedheadline, description, . . . ]

for var in vars:
    try:
        meta_dict[var] = html.find(var}).text
    except:
        meta_dict[var] = ''

Upvotes: 1

Related Questions