连接池请求Python

时间:2019-05-04 06:42:43

标签: python asynchronous python-requests

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

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

我编写了以下代码:

主循环:

threads = []
    for hour in schedule.each():
        if(int(hour.key()) in range(filtering_hour_start, filtering_hour_end)):
            for match in hour.val():
                process = Thread(target=analyse_fixture, args=(match, True, False, True, countries, ))
                process.start()
                threads.append(process)
                parse_mode=telegram.ParseMode.HTML)
    for process in threads:
        process.join()

使用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()

在analyse_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)

发出请求的检索页面方法

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,但似乎无法将其与我编写的代码混合

0 个答案:

没有答案