程序运行两次-正确使用主程序

时间:2018-11-22 16:44:45

标签: python function main

试图弄清楚为什么我的小队功能要运行两次。靠近底部的大多数函数都将前一个函数的数据作为输入,最后一个函数在列表列表中查找最大值。 我想在这里正确设置我的主设备,以便1)在该程序中一切正常运行,以及2)从另一个程序调用时,它将仅自行运行小队并将小队的值返回给该程序

在我的外队看来,小队跑了两次,而循环打印了“一次完成”-两次。话虽如此,我应该如何设置主电源?

这是如何工作的-

第一个函数小队返回列表列表
第二个函数Goals_convert_dash-从第一个函数(小队)获取输出-在列表列表中获取两个元素(每个列表),如果有破折号(-),则将其转换为零。 。第三功能-Goal_convert_toint从Goals_convert_dash获取输出并将第二列转换为零。
第四-get_max然后从第二列获取最大值,并将其与第一列零的数据一起输出

如果运行正常,我认为输出应该是-

一次完成 英超联赛最佳进球手是: ['John Smith',11]

我当前的输出是-

iter one done
Premier league top goal scorer is: 
['John Smith', 11]
iter one done

程序-

import requests
from bs4 import BeautifulSoup
import datetime
from operator import itemgetter
from scrape1_squad_urls import table

base_url = 'https://someurl.com'


# Squad Data

team_table = table()

#team_names = team_urls()
def squads():


    strip_team_name = [i.replace('https://www.someurl.com', '') for i in team_table]
    full_strip = [i.replace('-squad', '') for i in strip_team_name]

    iter = 0
    while iter < 1:
        tdstrip = []
        x = []
        tdstripclean = []
        for i in team_table:


                squad_r = requests.get(i)

        #        print(squad_r.status_code)

                now = datetime.datetime.now()
                soup = BeautifulSoup(squad_r.text, 'html.parser')

        #       div = soup.find('div', {'class': '-bp30-box col span1/1'})
                premier_squad_table = premier_squad_soup.find_all('table', {'class': 'table -small no-wrap football-squad-table '})
                headings = []
                playerurls = []
                full_links = []


                strip_team_name = [i.replace('https://someurl.com/', '') for i in team_table]
                full_strip = [i.replace('-squad', '') for i in strip_team_name]
                find_team_name = soup.find('span',{'class': 'swap-text__target'})
                for table in premier_squad_table:

                    table_head = table.find('thead')
                    head_rows = table_head.find_all('tr')

                    for item in head_rows:
                        headers = item.find_all('th')

                    table_body = table.find('tbody')
                    rows = table_body.find_all('tr')
                    for row in rows:
                        td = row.find_all('td')
                        x = [x.text.strip() for x in td]
                        position_teamname = (headers[0].text,find_team_name.text)
                        x[2]
                        x.extend(position_teamname)
                        tdstrip.append(x)
        iter = iter + 1
        print("iter one done")
    return(tdstrip)

#   return(newList)
#squads()

def goals_convert_dash():

    tdstrip = squads()  

    indexes = [0,2]
    newList = [[each_list[i] for i in indexes ] for each_list in tdstrip ]
    for eachlist in newList:
        if eachlist[1] == '-':
            eachlist[1] = 0
    return(newList) 
#goals_convert_dash()



#print(newList)

def goals_convert_toint():
    converted = goals_convert_dash()

    for eachlist in converted:
        eachlist[1] = int(eachlist[1])
    return(converted)        

#goals_convert_toint()


def get_max(lst, i, key_func=None):

    if not key_func: key_func = lambda x: x[i]
    res = max(lst, key=key_func)
    return [res[0], res[i]]

data = goals_convert_toint()

print("Premier league top goal scorer is: ")

print(get_max(data,1, lambda x: int(x[1])))

if __name__ == '__main__':
     squads()
#    goals_convert_dash()
#    goals_convert_toint()
#    get_max()

0 个答案:

没有答案