【问题标题】:How can i get all hrefs only from soup.find not find_all我怎样才能只从soup.find而不是find_all中获取所有href
【发布时间】:2023-04-05 16:53:02
【问题描述】:

我需要从 match_items 获取所有 href,我的代码:

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

【问题讨论】:

    标签: python parsing beautifulsoup href


    【解决方案1】:

    此代码从 match_items div 生成一个 href 列表。 每个 href 都以 '/matches/' 为前缀,我相信这就是你所追求的。

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

    【讨论】:

      【解决方案2】:

      您可以使用来自your last question 的结果或通过这行代码获取它:

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

      它选择包含<a> 的所有<div> 并使用列表解析语法遍历所有结果以创建带有url 的列表。

      【讨论】:

        猜你喜欢
        • 2018-03-02
        • 1970-01-01
        • 2019-01-12
        • 2020-10-07
        • 1970-01-01
        • 2023-01-16
        • 2021-10-26
        • 2021-08-16
        • 2021-04-22
        相关资源
        最近更新 更多