Home > Enterprise >  How can i get all hrefs only from soup.find not find_all
How can i get all hrefs only from soup.find not find_all

Time:10-13

I need get all hrefs from match_items, my code:

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")
match_info.append(match_items.findAll("a", class_="match a-reset", href=True).item['href'])```

CodePudding user response:

This code produces a list of hrefs from the match_items div. Each href is prefixed with '/matches/', which is what I believe you were after.

url_news = "https://www.hltv.org/matches"
response = requests.get(url_news)
soup = BeautifulSoup(response.content, "html.parser")
match_items = soup.find("div", {"class": "upcomingMatchesSection"})
match_info = [item["href"] for item in match_items.findAll("a", {"class": "match a-reset"})]

CodePudding user response:

You could use the result from your last question or get it with this line of code:

[link['href'] for link in soup.select("div.upcomingMatch a")]

It is selecting all <div> that includes an <a> and with list comprehension syntax iterate over all results to create a list with urls.

  • Related