Python 创建日期列表并插入URL

Python 创建日期列表并插入URL,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我对Python还比较陌生,所以请容忍我,但这里是我的问题。我有一个特定的日期列表,需要插入到URL中,然后在每个URL中循环以获取web数据。我也做过类似的任务,但在不需要创建列表的情况下。下面是一个例子 url_template = "https://www.basketball-reference.com/play- index/lineup_finder.cgi?request=1&match=single&player_id=&offset={set}" li

我对Python还比较陌生,所以请容忍我,但这里是我的问题。我有一个特定的日期列表,需要插入到URL中,然后在每个URL中循环以获取web数据。我也做过类似的任务,但在不需要创建列表的情况下。下面是一个例子

url_template = "https://www.basketball-reference.com/play-
index/lineup_finder.cgi?request=1&match=single&player_id=&offset={set}"

lineup_df = pd.DataFrame()

for set in range(0, 12600, 100):  # for each page
    url = url_template.format(set=set)  # get the url

page_request = requests.get(url)
soup = BeautifulSoup(page_request.text,"lxml")

column_headers = [th.getText() for th in 
        soup.findAll('tr', limit=2)[1].findAll('th')]

# get lineup data
data_rows = soup.findAll('tr')[2:] 
lineup_data = [[td.getText() for td in data_rows[i].findAll(['td','th'])]
        for i in range(len(data_rows))]

# Turn page data into a DataFrame
page_df = pd.DataFrame(lineup_data, columns=column_headers)

# Append to the big dataframe
lineup_df = lineup_df.append(page_df, ignore_index=True)

所以基本上我想要完成的是用一个日期列表来代替set in range。希望这是有道理的

您的代码在创建url的过程中运行,但它没有按照您的需要将其捕获到列表中,列表理解将完成这项工作。然后,您可以为创建的每个url运行url\u列表。关闭

url_template = "https://www.basketball-reference.com/playindex/lineup_finder.cgi?request=1&match=single&player_id=&offset={offset}"
url_list=[url_template.format(offset=offset) for offset in range(0, 12600, 100)]
for url in url_list:
    # the rest of code here

那么,你的代码有什么问题呢?我把这段代码作为我希望完成的任务的一个例子,除了“范围(012600100)”需要替换为2017年到2018年的大约100个日期。谢谢,这让我朝着正确的方向迈出了一步。