Search code examples
pythonparsingbeautifulsouphref

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


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

Solution

  • 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"})]