【发布时间】:2022-01-06 19:20:40
【问题描述】:
我正在尝试编写一个不需要每年都输入的代码,代码只是为我完成。我尝试使用 for 循环这样 for year in range(1995,2021): 但代码给了我以下错误
File "C:\Users\chadd\OneDrive\Desktop\Wind Spacecraft\Codes\get_files_wind_test.py", line 38, in <module>
sub_dir = year + '/'
# sub directory because there are several year in the parent directory
TypeError: unsupported operand type(s) for +: 'int' and 'str'
下面是我没有 for 循环的代码。在这里我手动输入年份,它会从网上下载文件并保存在特定的年份。我正在考虑用 for 循环替换 year=input("Enter the year:")。
###############
## Define sc and date range
###############
year = input("Enter the year: ") # takes the input
###############
## Define Paths
###############
external_url_base = 'https://cdaweb.gsfc.nasa.gov/pub/data/wind/waves/dust_impact_l3/' # url from where we need to scarp our data
sub_dir = year + '/' # sub directory because there are several year in the parent directory
url = external_url_base + sub_dir
local_dir_base = r'C:\Users\chadd\OneDrive\Desktop\Wind Spacecraft\Data' # this is my directory where files will be saved
sub_dir = '/'+ year + '/' # since there are years ranging from 1995 to 2020,as the input change different year files get stored in different year folder.
local_dir = local_dir_base + sub_dir # this line compiles local base and sub as one and this is the path that python uses to save files.
#########################
# Identify remote files #
#########################
## Read web page
resp = requests.get(url)
# create beautiful-soup object (all links on web page)
soup = BeautifulSoup(resp.content, 'html5lib')
## Error handle
if resp.status_code != 200:
print('**ERROR: No data available from then**')
resp.raise_for_status()
# create beautiful-soup object (all links on web page)
soup = BeautifulSoup(resp.content, 'html5lib')
# find all links on web-page
links = soup.findAll('a')
# filter the link sending with .cdf
cdf_files = []
for l in links:
if l['href'].endswith('cdf'):
#print(l['href'])
cdf_files.append(url + l['href'])
print(cdf_files)
###############
## Go get remote files, download locally
###############
# Iterate through list of files
for link in cdf_files:
#print(link)
# get the file name seperately
fn = link.split('/')[-1]
r = requests.get(link)
#print(r)
# Sub directory based on type of data being downloaded, to be saved in the Data directory. ex: TDS_files, QF_files, etc.
data_file = local_dir + fn
# Check if the file is already in that directory to avoice duplicates and uneccisary processing.
if path.exists(data_file):
print('Already have ',data_file, '.\nMoving on...')
continue
else:
print('Downloading ',data_file, '...')
with open(data_file, 'wb') as f:
f.write(r.content)
感谢任何帮助!
【问题讨论】:
-
您发布的代码运行没有错误。也许您在某个时候将 year 转换为 int?
-
这能回答你的问题吗? Python String and Integer concatenation
标签: python for-loop web-scraping