I do some telegram bot, and i need to get links from html.
I want to take href for Matches from this website https://www.hltv.org/matches
My previous code is
elif message.text == "Matches":
url_news = "https://www.hltv.org/matches"
response = requests.get(url_news)
soup = BeautifulSoup(response.content, "html.parser")
match_info = []
match_items = soup.find("div", class_="upcomingMatchesSection")
print(match_items)
for item in match_items:
match_info.append({
"link": item.find("div", class_="upcomingMatch").text,
"title": item["href"]
})
And i dont know how i can get links from this body.Appreciate any help
2
Answers
What happens?
You try to iterate over
match_items
but there is nothing to iterate, cause you only selected the section including the matches but not the matches itself.How to fix?
Select the upcomingMatches instead and iterate over them:
Getting the
url
you have to select an<a>
:Example
Output
You can try this out.
<div>
with classname asupcomingMatch
<div>
and from each<div>
, extract the match link which is present inside the<a>
tag with class name asmatch
.Here is the code: