[英]Python program which finds specific terms entered by a user in multiple HTML Files
設計了一個程序(例如 python),突出顯示哪個 html 文件包含用戶輸入的搜索詞。
我需要幫助編寫一個 python 程序,該程序在我自己制作的多個 HTML 文件中查找術語,並且該程序還說明了該術語出現在 HTML 文件中的次數。 該術語可由用戶輸入。
老實說,我不知道從哪里開始。
如果您的 HTML 文件存儲在本地:
word = input('Word to search for: ')
html_files = ['first.html', 'second.html']
for i in html_files:
with open(i, 'r') as f:
matches = {index: x.strip() for index, x in enumerate(f.readlines()) if word in x}:
print('Found {} matches on {}:'.format(len(matches), i))
for n, line in matches.items():
print('Line {}: {}'.format(n, line))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.