连接池请求Python

2024-04-27 19:04:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Python同时发送许多请求,但出现以下错误:

2019-05-04 08:32:04,871 - telegram.vendor.ptb_urllib3.urllib3.connectionpool - WARNING - Connection pool is full, discarding connection: <<host>>

我写了以下代码:

主回路:

^{pr2}$

使用retrieve_page_v2并因此发出请求的方法之一:

def analyse_fixture(fixture, live=False, upcoming=False, odds=False, countries={}):
    base_url = "https://www.flashscore.com/match/" + fixture + "/"
    summary_url = "https://d.flashscore.com/x/feed/d_su_" + fixture + "_en_1"
    statistics_url = "https://d.flashscore.com/x/feed/d_st_" + fixture + "_en_1"
    h2h_url = "https://d.flashscore.com/x/feed/d_hh_" + fixture + "_en_1"

    base_soup = retrieve_page_v2(base_url)
    h2h_soup = retrieve_page_v2(h2h_url, True)
    summary_soup = retrieve_page_v2(summary_url, True)
    home_overal = head2heads[0]
    away_overal = head2heads[1]
    h2h = head2heads[2]

    home_matches = home_overal.find('table').find('tbody').find_all('tr')
    away_matches = away_overal.find('table').find('tbody').find_all('tr')
    if(len(home_matches) > 10 and len(away_matches) > 10):
        home_overal_details_process = Thread(target=analyse_details, args=(home_matches, home, away, statistics_in_league, "home_matches"))
        away_overal_details_process = Thread(target=analyse_details, args=(away_matches, away, home, statistics_in_league, "away_matches"))

        home_overal_details_process.start()
        away_overal_details_process.start()
    else:
        exit()


    home_home_matches = h2h_soup.find('div', {'id': 'tab-h2h-home'}).find('table').find('tbody').find_all('tr')
    away_away_matches = h2h_soup.find('div', {'id': 'tab-h2h-away'}).find('table').find('tbody').find_all('tr')
    if(len(home_home_matches) > 10 and len(away_away_matches) > 10):
        home_home_details_process = Thread(target=analyse_details, args=(home_home_matches, home, away, statistics_in_league, "home_home_matches"))
        away_away_details_process = Thread(target=analyse_details, args=(away_away_matches, away, home, statistics_in_league, "away_away_matches"))

        home_home_details_process.start()
        away_away_details_process.start()
    else:
        exit()

    away_away_details_process.join()
    home_home_details_process.join()
    away_overal_details_process.join()
    home_overal_details_process.join()

从analyze_fixture内部调用的方法,它也使用retrieve_page_v2

def analyse_details(matches, team, team_2, statistics, name=""):
    for match in matches[0:10]:
        match_code = match.get('onclick')[17:25]
        if(db.child("matches").child(match_code).child('total_goals').get().val() == None or db.child("matches").child(match_code).child("total_corners").get().val() == None or db.child("matches").child(match_code).child('winner').get().val() == None or db.child("matches").child(match_code).child('fh_winner').get().val() == None):
            home_corners = "Unknown"
            away_corners = "Unknown"
            total_corners = "Unknown"

            base_url = "https://www.flashscore.com/match/" + match_code
            summary_url = "https://d.flashscore.com/x/feed/d_su_" + match_code + "_en_1"
            statistics_url = "https://d.flashscore.com/x/feed/d_st_" + match_code + "_en_1"
            base_soup = retrieve_page_v2(base_url)
            summary_soup = retrieve_page_v2(summary_url, True)

            teams = base_soup.find_all('a', {'class': 'participant-imglink'})

            if(statistics):
                statistics_soup = retrieve_page_v2(statistics_url, True)

发出请求的retrieve page方法

def retrieve_page_v2(url, use_headers=False):
    headers = {
        "User-Agent": "core",
        "Accept": "*/*"
        "Accept-Language:" "*",
        "X-Requested-With": "XMLHttpRequest",
        "Connection": "keep-alive"
    }
    if(use_headers):
        response = requests.get(url, headers=headers)
    else:
        response = requests.get(url)

    return BeautifulSoup(response.content, features="lxml")

有人能帮我解决这个问题吗?我尝试过使用aiohttp,但似乎无法将其融入我编写的代码中


Tags: childurlhomematchpagefinddetailsprocess