在 excel 行循环的拆分中我做错了什么?

What am I doing wrong in this split for excel rows loop?

我编写了这个小代码来从 excel 工作簿中的 url 列表中拆分域。但问题是我无法将其写入实际工作簿,甚至无法创建包含域的新工作簿。

#looping it
for i in range(2, 200):
print((sheet.cell(row=i, column=1).value).split('http://')[-1].split('/')[0].split('www.')[-1])

#but by this i have to go copy paste the results in the excel sheet
#so i tried this replace value method but it keeps showing attribute error

for x in range(2, 258):
    sheet.cell(row=x, column=1).value = sheet.cell(row=x, column=1).value.split('http://')[-1].split('/')[0].split('www.')[-1]


Traceback (most recent call last):
  File "<pyshell#63>", line 2, in <module>
    sheet.cell(row=x, column=1).value = sheet.cell(row=x, column=1).value.split('http://')[-1].split('/')[0].split('www.')[-1]
AttributeError: 'NoneType' object has no attribute 'split'

我希望这个循环遍历列表并将 url(https://www.example.com/example-page/) 拆分到域 (example.com) 并保存它在同一个 sheet 或一个新的 sheet 中,当我 使用这个

wb.save('domains_list')
#it saves the splitted domains automatically without me copy pasting it from idle to excel workbook.

如评论中所述,您必须处理单元格为空的情况:

for i in range(2, 200):
    val = sheet.cell(row=i, column=1).value
    if val is None:
        continue   
    print(val.split('http://')[-1].split('/')[0].split('www.')[-1])