我尝试使用两个for循环从API请求中提取所有数据,其中循环1)每天都从API请求数据-而嵌套在该循环中-循环2)遍历列表中的每个页面我要获取数据的URL。
我遇到的问题是它会整天循环播放,只是只会返回URL数组中最后一个列表的数据。
pages = read_page_paths_from_file(PAGE_FILTERS, PROPERTY_URI) # stores in a list
start_date = datetime.strptime(START_DATE, "%Y-%m-%d")
end_date = datetime.strptime(END_DATE, "%Y-%m-%d")
output_rows = []
for day in date_range(start_date, end_date, delta=timedelta(days=1)):
day = day.strftime("%Y-%m-%d")
for i in range(len(pages)):
request = {
'startDate': day,
'endDate': day,
'dimensions': ['date','query','page'],
'rowLimit': 1000,
'dimensionFilterGroups' : [
{
"filters": [
{
"dimension": "page",
"expression": pages[i]
}
]
}
]
}
response = execute_request(webmaster_service, PROPERTY_URI, request)
time.sleep(1)
if response is None:
logging.error("Request failed %s", json.dumps(request, indent=2))
continue
for row in response['rows']:
keys = ','.join(row['keys'])
date, query, page = keys.split(",")
output_row = [date, query, page, row['clicks'], row['impressions'], row['ctr'], row['position']]
output_rows.append(output_row)
我想要获取列表中每个URL的数据以及日期范围内每天的URL数据。
但是,我得到的是所有7天的数据,但仅显示列表中的最后一个URL。