Digital Farmer
Digital Farmer

Reputation: 2147

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 IndentationError: expected an indented block or says I'm trying to use find instead of find_all.

How should I proceed to collect all href?


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')

jogo = jogos[0]
linksgame = jogo.find('a', href=True).attrs['href'].strip()

print(linksgame)

Upvotes: -1

Views: 38

Answers (1)

MendelG
MendelG

Reputation: 20118

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])

Upvotes: 1

Related Questions