How to Select All Elements Based on Attribute Name



  • I want to parse a website where there is a table and I want to pull out data from there, but the problem is that I dont know how to refer to a spesific cell of the table in the find_all function. For example:

    <td class="... " xyz="opponent"><a href="...">asdf</a></td>

    And I want to find all tds which has the attribute xyz.

    Thx



  • Hey @mrnuqname

    Thanks for posting.

    I am not sure what exactly you are referring to when you say "data". Do you mean the content of the td element ?

    But is a way to get all elements based on attribute name. In this case the attribute name is "xyz".

    document.querySelectorAll('[xyz]');

    This will give you all elements with the provided attribute name in an array format.

    If you want to get the content of the td element e.g. <a href="...">asdf</a>

    then just call on the .innerHTML method. So document.querySelectorAll('[xyz]')[0].innerHTML; In this example it would get you the innerHTML for the first element only. ([0])

    Keep in mind you might also be able to use document.getElementsByClassName("example") which returns a collection (array) of elements with the provided class name.

    I hope this info is of some use.



  • Thanks for the response!!


Log in to reply