在 while 循环中调用 API 不会将所有内容写入 txt 文件

问题描述 投票:0回答:0

完整程序将调用 NYT API 以查找 1851 年至今的文章。为了测试,我将它限制在 1851 年,今年只有四个月有问题,所以 1-8 个月应该只返回空白 txt 文件。问题是只有 11 月和 12 月被写入他们的文件,而不是 9 月和 10 月。您需要注册一个 NYT API 密钥并创建一个名为 txt_holder 的目录。

#create a NYT api call to scrap every year and all its months into individual txt files so that I can do a sentiment analysis month by month


#imports request function
import requests
#gets the datetime so I can get the current year
import datetime

import time

API_KEY = 

def files_and_count():
  #NYT data goes back to the year 1850
  year_increment = 1850
  today = datetime.date.today()
  year = today.year

  months = 0
  
  while year_increment != 1851:
    year_increment = year_increment + 1
    
    while months != 12:
      months = months+1
      query = str(year_increment) + "/"+ str(months)
      real_file_name = str(year_increment) + "_"+ str(months)
      file_name = str('txt_holder/'+ real_file_name +'.txt')
      file = open(file_name, 'w')
      
      search(query,API_KEY,file)

    
    months = 0

#Function that takes the API query and writes them into a file
def search(query,API_KEY,file):
  #here is the url with the query and API key as values so it is more flexible
  url = f'https://api.nytimes.com/svc/archive/v1/{query}.json?api-key={API_KEY}'
  
  print(url)

  #time between each API call
  time.sleep(12)
  
  try:
    #request the url
    response = requests.get(url)
  
    #find the JSON content
    content = response.json()
  
    #for loop to go through the text the request pulled from NYT
    for item in  content["response"]["docs"]:
      #searches in the JSON for the titles of the articles
      text = item["headline"]["main"]
      #for every article title there is a newline character added to the end of it so that each title is on a new line
      file.write(text+"\n")
      
  except:
    print('failed')
  
files_and_count()

这是对我有用的,谢谢你的帮助:

    #imports request function
import requests
#gets the datetime so I can get the current year
import datetime

import time

API_KEY = 


def files_and_count():
  #NYT data goes back to the year 1850

  today = datetime.date.today()
  year = today.year

  for i in range(2022, year):

    for months in range(1,13):
      
      query = str(year) + "/"+ str(months)
      real_file_name = str(year) + "_"+ str(months)
      file_name = str('txt_folder/'+ real_file_name +'.txt')
      file = open(file_name, 'w')
      
      search(query,API_KEY,file)




#Function that takes the API query and writes them into a file
def search(query,API_KEY,file):
  #here is the url with the query and API key as values so it is more flexible
  url = f'https://api.nytimes.com/svc/archive/v1/{query}.json?api-key={API_KEY}'
  
  print(url)

  #time between each API call
  time.sleep(12)
  
  try:
    #request the url
    response = requests.get(url)
  
    #find the JSON content
    content = response.json()
  
    #for loop to go through the text the request pulled from NYT
    for item in  content["response"]["docs"]:
      #searches in the JSON for the titles of the articles
      text = item["headline"]["main"]
      #for every article title there is a newline character added to the end of it so that each title is on a new line
      file.write(text+"\n")
      
  except:
    print('failed')
  




files_and_count()
python database api file text
© www.soinside.com 2019 - 2024. All rights reserved.