Find string between two sets of characters or 3rd and 4th quotation marks

Viewed 23

I have been playing with Beautifulsoup and re to collect only the links I need from a webpage.

I was able to cut the page content to a <class 'bs4.element.ResultSet'>

This dataset contains the names of athletes and links to their profiles.

Now, I am using a for loop to print out the information of each individual.

The type of each iteration of the for loop is <class 'bs4.element.Tag'>

Each element in the for loop looks something like this

</span>, <span class="txt-blue">
<a href="https://www.eliteprospects.com/player/70215/riley-nash">Riley Nash (F)</a>
              <a href="https://www.eliteprospects.com/league/wjc-18-w/team-captaincy/2021-2022">“A”</a>

I would like to extract only the first link https://www.eliteprospects.com/player/70215/riley-nash but not the 2nd.

I was hoping I could define <a href= as the start and the first occurrence of >as the end to extract the first link.

I also tried using the fact that the first link of each iteration of the for loop is going to be sandwiched between the 3rd and 4th quotation marks.

I have looked into reg and other methods and tried converting <class 'bs4.element.Tag'> to str but still wasn't able to figure out how to extract links.

1 Answers

If my_result_set holds your <class 'bs4.element.ResultSet'> you can do:

for tag in my_result_set:
    print(tag.a["href"])

This should print the href= of first <a> tag.

Related