Search code examples
pythonattributesscreen-scraping

Extracting the value of an attribute with a hyphen using Beautiful Soup 4


I am traversing through my tags using beautifulsoup 4. I have the following tag contents and am unable to extract the atttribute value of the 'data-event-name' attribute. I want '15:02' from this.

This is the html I need to extract 15:02 from

I have tried many many things but am unable to get this value. I tried using the re package, getattr python, find, find_all, etc, etc. this is one example of something I tried:

for racemeetnum,r1_a in enumerate(r1, start=1):
    event1 = getattr(r1_a, 'data-event-name') # doesnt work

<


Solution

  • Thank you @Jack Fleming. I managed to sort this last night. In the end my issue wasn't that I couldn't find the attribute, it was that I wasn't trapping the errors when the attribute wasn't found. I surrounded code with a try/except and it worked fine.

    Thanks for responding!