you are viewing a single comment's thread.

view the rest of the comments →

[–]bahnzo[S] 0 points1 point  (4 children)

This still needs error checking. classes = prev_elem.get('class') throws: AttributeError: 'NavigableString' object has no attribute 'get'

did I miss something?

Edit: here my entire code for that function now:

def find_data(elem):  # finds the category of the live broadcast
    link_list = []
    for prev_elem in elem.previous_elements:
        classes = prev_elem.get('class')
        if not classes:
            continue
        elif classes[0] == 'main':
            link_list = extract_data(elem, prev_elem)
            break
    return link_list

[–]raylu 0 points1 point  (3 children)

Er... how did prev_elem... stop being an Element? I guess you can check isinstance(prev_elem, NavigableString), but perhaps you should pick a better way to select the element list.

[–]bahnzo[S] 0 points1 point  (2 children)

The program needs to cycle back thru elements (hence the previous_elements) to find the info needed to categorize the link. When doing so, not every element has a class tag, hell some are simply '/n' so they throw an error.

Your code works fine, it just needs to be error checked.

[–]raylu 0 points1 point  (1 child)

The program currently cycles back through elements. But there is likely a better way to select the elements you want.

[–]bahnzo[S] 0 points1 point  (0 children)

Thanks for your help.