如何从ROW获取HREF

How i can get href from row(如何从ROW获取HREF)

本文介绍了如何从ROW获取HREF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些电报机器人,我需要从html获得链接。 我想从这个网站获取比赛的HREFhttps://www.hltv.org/matches

我之前的代码是

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

            })

我不知道如何从此正文获取链接。感谢任何帮助

推荐答案

发生了什么?

您尝试迭代match_items,但没有要迭代的内容,因为您只选择了包含匹配项的节,而没有选择匹配项本身。

如何修复?

改为选择upcomingMatches并迭代它们:

match_items = soup.select("div.upcomingMatchesSection div.upcomingMatch")

要获得url,您必须选择一个<a>

item.a["href"]

示例

from bs4 import BeautifulSoup as bs
import requests


url_news = "https://www.hltv.org/matches"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}

response = requests.get(url_news, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
match_info = []
match_items = soup.select("div.upcomingMatchesSection div.upcomingMatch")

for item in match_items:
    match_info.append({
            "title": item.get_text('|', strip=True),
            "link": item.a["href"]

    })
match_info

输出

[{'title': '09:00|bo3|1WIN|K23|Pinnacle Fall Series 2|Odds',
  'link': '/matches/2352066/1win-vs-k23-pinnacle-fall-series-2'},
 {'title': '09:00|bo3|INDE IRAE|Nemiga|Pinnacle Fall Series 2|Odds',
  'link': '/matches/2352067/inde-irae-vs-nemiga-pinnacle-fall-series-2'},
 {'title': '10:00|bo3|OPAA|Nexus|Malta Vibes Knockout Series 3|Odds',
  'link': '/matches/2352207/opaa-vs-nexus-malta-vibes-knockout-series-3'},
 {'title': '11:00|bo3|Checkmate|TBC|Funspark ULTI 2021 Asia Regional Series 3|Odds',
  'link': '/matches/2352092/checkmate-vs-tbc-funspark-ulti-2021-asia-regional-series-3'},
 {'title': '11:00|bo3|ORDER|Alke|ESEA Premier Season 38 Australia|Odds',
  'link': '/matches/2352122/order-vs-alke-esea-premier-season-38-australia'},...]

这篇关于如何从ROW获取HREF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从ROW获取HREF

基础教程推荐