首页 > 解决方案 > 如何在beautifulsoup中找到列表的父母

问题描述

import requests
from bs4 import BeautifulSoup

url ="https://www.hltv.org/stats/matches/mapstatsid/103093/furia-vs-chaos"
headers= {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}

response = requests.get(url, headers = headers)
soup = BeautifulSoup(response.content, "html.parser")
#stat_tables = soup.find_all("table", class_="stats-table")



results = {}

all_results = soup.find_all("div", class_="round-history-half")
for partial_result in all_results:
    half_results = partial_result.find_all("img")
    for result in half_results:
        if (result["title"]):
            rounds_won = result["title"].split("-")
            key = int(rounds_won[0]) + int(rounds_won[1])
            results[key] = result["title"]

for key in sorted(results):
    print(key, results[key])


这段代码给了我比赛的所有分数,我想找到父母,比如说 print(results[4]),看看谁赢了。我不知道如何从列表中找到父母。

结果应该是

<img alt="FURIA" src="https://static.hltv.org/images/team/logo/8297" class="round-history-team" title="FURIA">

或者

Furia

标签: pythonhtmlbeautifulsoupparent

解决方案


用这个,

# retrieve the parent first.
divs = soup.find_all("div", class_="round-history-team-row")

for div in divs:
    parent_img_title = div.find('img', class_="round-history-team")['title']
    print("Title : " + parent_img_title)

    for result in div.find_all("img", class_="round-history-outcome"):
        if result["title"]:
            rounds_won = result["title"].split("-")
            key = int(rounds_won[0]) + int(rounds_won[1])
            print("Key %d" % key)
            results[key] = result["title"]

输出,

Title : FURIA
Key 4
Key 5
...
Title : Chaos
Key 1
Key 2

推荐阅读