爲了另一個提示添加到弗雷德裏克的答案,你也可以得到來自搜索引擎如代碼,像這樣的啓發:
def dosearch(terms, searchtype, case, adddir, files = []):
found = []
if files != None:
titlesrch = re.compile('>title<.*>/title<')
for file in files:
title = ""
if not (file.lower().endswith("html") or file.lower().endswith("htm")):
continue
filecontents = open(BASE_DIR + adddir + file, 'r').read()
titletmp = titlesrch.search(filecontents)
if titletmp != None:
title = filecontents.strip()[titletmp.start() + 7:titletmp.end() - 8]
filecontents = remove_tags(filecontents)
filecontents = filecontents.lstrip()
filecontents = filecontents.rstrip()
if dofind(filecontents, case, searchtype, terms) > 0:
found.append(title)
found.append(file)
return found
來源和更多信息:http://www.zackgrossbart.com/hackito/search-engine-python/
問候,
最大
你想得到什麼結果?你想要在整個字符串中查找所有這些變體? – JMax
我想將這些組合成一個組合對象,並在添加到數據庫時執行檢查。 –