【问题标题】:How to use for loop to scrape data for 15 years?如何使用for循环抓取15年的数据?
【发布时间】: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)

感谢任何帮助!

【问题讨论】:

标签: python for-loop web-scraping


【解决方案1】:

这里的年份是一个int(一个数字),您需要使用sub_dir = str(year) + '/'将它转换(转换)为字符串(文本)。

这应该可以解决你在 for 循环中的问题。

【讨论】:

  • 它给了我这个错误,只能将str(不是“int”)连接到str。
  • 正如 luk2302 在 cmets 中所说,您不能连接 str 和 int 类型。只需将年份(从 for 循环)转换为 str。
【解决方案2】:

错误

+ 是“操作员”。也就是说,它对它两边的东西进行操作。我们称这些东西为“操作数”。因此错误的措辞。

左边是int(整数),右边是str(一些文字)。虽然我们人类并没有真正将数字和符号视为完全不同的东西,但它们对于计算机来说却是完全不同的东西。文本符号“1”与实际的数字 1 完全不同。所以,对于计算机来说,words plus 123 是没有意义的。

因此,您需要告诉 Python 将您的数字(年份)转换为文本。最快的方法是将号码包含在 str() 中。

修复它

tl;dr:将第 38 行更改为: sub_dir = str(year) + '/'

您还必须将第 42 行编辑为 sub_dir = '/'+ str(year) + '/'

有时,Python 会很聪明,会自动将事物转换为正确的类型(可能是因为其他程序员已经完成了上述操作并添加了这种转换),但在这种情况下却不是。

【讨论】:

  • 感谢您的解释和解决方案。
猜你喜欢
  • 2021-05-11
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2021-07-27
  • 1970-01-01
相关资源
最近更新 更多