以下是修正后的代码:

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程序员检测以下代码错误及修正并提示 with openresultscsv a+ as f for row in results domain = rowsplit0 if domain startswithhttp

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

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