How to collect all specified hrefs?

2024/11/16 8:45:38

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 BeautifulSoupurl = '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)
Answer

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

import requests
from bs4 import BeautifulSoupurl = "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])
https://en.xdnf.cn/q/119212.html

Related Q&A

How do I sort data highest to lowest in Python from a text file?

I have tried multiple methods in doing this but none of them seem to work The answer comes up alphabetically insteadf=open("class2.txt", "r") scores=myfile.readlines() print(sorted(…

Determine if a variable is an instance of any class

How to determine if a variable is an instance in Python 3? I consider something to be an instance if it has __dict__ attribute.Example:is_instance_of_any_class(5) # should return False is_instance_of…

Method POST not allowed with Django Rest Framework

Im trying to create a JSON API compliant rest service using Django Rest Framework JSON API: https://django-rest-framework-json-api.readthedocs.io/en/stable/index.htmlI think Im stuck at the Django Rest…

Running multiple queries in mongo`

I have a collection and want to get a set of results that met a set of conditions. I understand the Mongo doesnt let you use joins so I would need to run separate queries and join the results into a si…

Error in Calculating neural network Test Accuracy

I tried to train my neural network, and then evaluate its testing accuracy. I am using the code at the bottom of this post to train. The fact is that for other neural networks, I can evaluate the testi…

Adding stats code to a function in Python

Im relatively new to Python and trying to learn how to write functions. The answer to this post highlights how to get certain stats from a dataframe and I would like to use it in a function.This is my…

Multiple Histograms, each for a label of x-axis, on the same graph matplotlib

I am trying to plot a graph to show different behaviors by males and females with regards to a certain activity, for different age-groups. So, if the age groups are: [1-10,11-20,21-30...] I would like …

How can I split a string in Python? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicates:Split python string every nth character?What is the most “pythonic” way to iterate over a list in chu…

How to enable media device access in Edge browser using Selenium?

I have a Selenium test I want to run in Edge against a page which uses the webcam and microphone. In order for those media devices to work in the browser, the user has to allow the site access to the d…

Print specific rows that are common between two dataframes

i have a dataframe (df1) like this id link 1 google.com 2 yahoo.com 3 gmail.comi have another dataframe(df2) like this: id link numberOfemployees 1 linkedin.com …