Total URL Link Counter in a File

  • Share this:

Code introduction


This code defines a function `main` that calculates and prints the total number of URL links in a specified file. It first reads the content of the file, then extracts all the links, and finally returns the number of links.


Technology Stack : os, re

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import re

def read_file(file_path):
    """
    读取文件内容,并返回内容列表。
    """
    if not os.path.exists(file_path):
        return []
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.readlines()
    return content

def extract_links(content):
    """
    从内容中提取所有URL链接。
    """
    links = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', content)
    return links

def count_links(file_path):
    """
    计算指定文件中所有URL链接的数量。
    """
    content = read_file(file_path)
    links = extract_links(''.join(content))
    return len(links)

def main():
    file_path = 'example.txt'
    link_count = count_links(file_path)
    print(f"Total number of links in the file: {link_count}")

# 运行主函数
if __name__ == "__main__":
    main()                
              
Tags: