📅  最后修改于: 2023-12-03 15:12:05.374000             🧑  作者: Mango
本程序用于计算给定链接列表中元音和辅音的数量。程序支持输入任意数量的链接,并返回它们中每个链接对应的元音和辅音数量。
pip install -r requirements.txt
)。python count_vowels_and_consonants.py
。link1,link2,link3
,例如https://www.example.com,https://www.test.com,https://www.demo.com
。import re
def count_vowels_and_consonants(link):
vowels = "AEIOUaeiou"
consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"
response = requests.get(link)
text = response.text
text = re.sub(r'<.*?>', '', text)
vowels_count = len(re.findall(f'[{vowels}]', text))
consonants_count = len(re.findall(f'[{consonants}]', text))
return vowels_count, consonants_count
if __name__ == "__main__":
import requests
import argparse
parser = argparse.ArgumentParser(description='Count vowels and consonants in given links.')
parser.add_argument('links', type=str, help='list of links, separated by comma (e.g. "link1,link2,link3")')
args = parser.parse_args()
links = args.links.split(',')
total_vowels_count = 0
total_consonants_count = 0
for link in links:
vowels_count, consonants_count = count_vowels_and_consonants(link)
total_vowels_count += vowels_count
total_consonants_count += consonants_count
print(f"{link}: {vowels_count} vowels, {consonants_count} consonants.")
print(f"\nTotal vowels: {total_vowels_count}.")
print(f"Total consonants: {total_consonants_count}.")
程序运行后,示例返回结果如下:
https://www.example.com: 782 vowels, 2157 consonants.
https://www.test.com: 529 vowels, 1857 consonants.
https://www.demo.com: 602 vowels, 1979 consonants.
Total vowels: 1913.
Total consonants: 5993.
返回结果以markdown格式输出,易于查看和复制。