Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python脚本中循环,仅获取最后的结果_Python_Loops - Fatal编程技术网

在Python脚本中循环,仅获取最后的结果

在Python脚本中循环,仅获取最后的结果,python,loops,Python,Loops,为什么我只能从玩家名称中的最后一个玩家那里获得数据 我想从所有球员的名字里得到数据 import csv import requests from bs4 import BeautifulSoup import urllib PLAYER_NAME = ["andy-murray/mc10", "rafael-nadal/n409"] URL_PATTERN = 'http://www.atpworldtour.com/en/players/{}/player-stats?year=0&

为什么我只能从玩家名称中的最后一个玩家那里获得数据

我想从所有球员的名字里得到数据

import csv
import requests
from bs4 import BeautifulSoup
import urllib

PLAYER_NAME = ["andy-murray/mc10", "rafael-nadal/n409"]
URL_PATTERN = 'http://www.atpworldtour.com/en/players/{}/player-stats?year=0&surfaceType=clay'
for item in zip (PLAYER_NAME):
    url = URL_PATTERN.format(item)

    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html)
    table = soup.find('div', attrs={'class': 'mega-table-wrapper'})

    list_of_rows = []
    for row in table.findAll('tr'):
        list_of_cells = []
        for cell in row.findAll('td'):
            text = (cell.text.encode("utf-8").strip())
            list_of_cells.append(text)
        list_of_rows.append(list_of_cells)


outfile = open("./tennis.csv", "wb")
writer = csv.writer(outfile)
writer.writerow(["Name", "Stat"])
writer.writerows(list_of_rows)

如评论中所述,您每次都在重新创建
行列表
。要解决这个问题,您必须将它移到for循环之外,而不是附加到它,并将其转换为列表列表,而是扩展它

另一方面,您的代码还有一些其他问题:

  • zip
    是多余的,它实际上会将您的姓名转换为元组,这会导致格式错误,您只需在
    PLAYER\u NAME
    上进行迭代,在进行此操作时,可能会将其重命名为
    PLAYER\u NAME
    (因为这是一个姓名列表)
  • 当试图格式化字符串时,只有空大括号,其中需要一个数字来指定参数在
    格式
    -在本例中为
    {0}


如评论中所述,您每次都在重新创建
行列表
。要解决这个问题,您必须将它移到for循环之外,而不是附加到它,并将其转换为列表列表,而是扩展它

另一方面,您的代码还有一些其他问题:

  • zip
    是多余的,它实际上会将您的姓名转换为元组,这会导致格式错误,您只需在
    PLAYER\u NAME
    上进行迭代,在进行此操作时,可能会将其重命名为
    PLAYER\u NAME
    (因为这是一个姓名列表)
  • 当试图格式化字符串时,只有空大括号,其中需要一个数字来指定参数在
    格式
    -在本例中为
    {0}


你在
PLAYER\u NAME
的每次迭代中都在重新创建
list\u行
。我该如何解决这个问题?将list\u行定义移到循环之外你在
PLAYER\u NAME
的每次迭代中都在重新创建
list\u行
。我该如何解决这个问题?将list\u行定义移到循环之外,好吗谢谢你们的反馈和建议。谢谢你们的反馈和建议。
PLAYER_NAMES = ["andy-murray/mc10", "rafael-nadal/n409"]
URL_PATTERN = 'http://www.atpworldtour.com/en/players/{0}/player-stats?year=0&surfaceType=clay'
list_of_rows = []
for item in PLAYER_NAMES:
    url = URL_PATTERN.format(item)

    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html)
    table = soup.find('div', attrs={'class': 'mega-table-wrapper'})

    # for row in table.findAll('tr'):
    #     list_of_cells = []
    #     for cell in row.findAll('td'):
    #         text = (cell.text.encode("utf-8").strip())
    #         list_of_cells.append(text)
    #     list_of_rows.extend(list_of_cells) # Change to extend here

    # Incidentally, the for loop above could also be written as:
    list_of_rows += [
        [cell.text.encode("utf-8").strip() for cell in row.findAll('td')]
        for row in table.findAll('tr')
    ]