Skip to content Skip to sidebar Skip to footer

How To Collect All Specified Href's?

In this test model I can collect the href value for the first ('tr', class_='rowLive'), I've tried to create a loop to collect all the others href but it always gives IndentationEr

Solution 1:

jogos returns a list, you can loop over it and find() an a for every iteration:

import requests
from bs4 import BeautifulSoup

url = "http://sports.williamhill.com/bet/pt/betlive/9"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}

site = requests.get(url, headers=headers)
soup = BeautifulSoup(site.content, "html.parser")
jogos = soup.find_all("tr", class_="rowLive")

for tag in jogos:
    print(tag.find("a", href=True)["href"])

Or:

print([tag.find("a", href=True)["href"] for tag in jogos])

Post a Comment for "How To Collect All Specified Href's?"