使用下面的代码,把大量url复制到2.txt中,然后运行脚本生成1.txt
import re
# 打开第二个文件以读取url
with open('2.txt', 'r') as input_file:
# 打开第一个文件以写入处理后的url
with open('1.txt', 'w') as output_file:
# 逐行读取第二个文件中的url
for line in input_file:
# 使用正则表达式检查url是否已经以http://或https://开头
if not re.match(r'^https?://', line):
# 如果url不是以http://或https://开头,则添加http://并写入到第一个文件中
line = 'http://' + line
# 将处理后的url写入第一个文件
output_file.write(line)