123456789101112131415161718192021222324252627 |
- # 输入文件,包含URL的内容
- input_file = 'output9.txt'
- # 输出文件,用于存放提取出的URL
- output_file = 'extracted_urls.txt'
- # 存放提取出的URL的列表
- extracted_urls = []
- # 读取输入文件并提取URL
- with open(input_file, 'r') as file:
- # 读取整个文件内容
- data = file.read()
- # 使用正则表达式提取URL
- import re
- urls = re.findall(
- r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', data)
- extracted_urls.extend(urls)
- # 将提取出的URL写入输出文件
- with open(output_file, 'w') as file:
- for url in extracted_urls:
- # 每个URL单独一行
- file.write(url + '\n')
- print("提取出的URL已保存到输出文件中。")
|