以下是修正后的代码:

with open('results.csv', 'a+') as f:
    for row in results:
        domain = row.split(',')[0]
        if domain.startswith('http://'):
            domain = domain.replace('http://', '')
        elif domain.startswith('https://'):
            domain = domain.replace('https://', '')
        row = [domain] + row.split(',')[1:]
        f.write(','.join(row) + '\n')
print('第{}页检索结果储存成功'.format(page))

修正说明:

  1. if domain startswith('http://'): 错误,应为 if domain.startswith('http://'):,即缺少了一个字母。同理,elif domain.startswith('https://'): 也需要修改。

  2. 在进行域名替换后,需要将新的域名和原来的数据拼接起来,即 row = [domain] + row.split(',')[1:]

  3. print('第{}页检索结果储存成功'.format(page)) 没有问题。

Python 代码错误修复:去除域名协议并更新数据

原文地址: https://www.cveoy.top/t/topic/olVF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录