如何从源代码(python 3)中查找单词的变体
我必须编写一个程序,接受用户输入的网站和关键字,然后读取该词的网站源代码。我必须对它进行编码,这样它才能检测到这个词的许多变体(例如,hello vs.hello,vs.hello!),但我不知道如何做到这一点。到目前为止,我已经对它进行了这样的编码,以检测准确的输入,但我不确定如何获得多个变体。任何帮助都将不胜感激,谢谢如何从源代码(python 3)中查找单词的变体,python,python-3.6,Python,Python 3.6,我必须编写一个程序,接受用户输入的网站和关键字,然后读取该词的网站源代码。我必须对它进行编码,这样它才能检测到这个词的许多变体(例如,hello vs.hello,vs.hello!),但我不知道如何做到这一点。到目前为止,我已经对它进行了这样的编码,以检测准确的输入,但我不确定如何获得多个变体。任何帮助都将不胜感激,谢谢 def main(): [n,l]=user() print("Okay", n, "from", l, ", let's get started.")
def main():
[n,l]=user()
print("Okay", n, "from", l, ", let's get started.")
webname=input("What is the name of the website you wish to browse? ")
website=requests.get(input("Please enter the URL: "))
txt = website.text
list=txt.split(",")
print(type(txt))
print(type(list))
print(list[0:10])
while True:
numkey=input("Would you like to enter a keyword? Please enter yes or no: ")
if numkey=="yes":
key=input("Please enter the keyword to find: ")
else:
newurl()
break
find(webname,txt,key)
def find(web,txt,key):
findtext=txt
list=findtext.split(sep=" ")
count = 0
for item in list:
if item==key:
count=count+1
print("The word", key, "appears", count, "times on", web)
def newurl():
while True:
new=input("Would you like to browse another website? Please enter yes or no: ")
if new=="yes":
main()
else:
[w,r]=experience()
return new
break
def user():
name=input("Hello, what is your name? ")
loc=input("Where are you from? ")
return [name,loc]
def experience():
wordeval=input("Please enter 3 words to describe the experience, separated by spaces (ex. fun cool interesting): ")
list=wordeval.split(sep=" ")
rate=eval(input("Please rate your experience from 1-10: "))
if rate < 6:
print("We're sorry you had a negative", list[0], "and", list[2], "experience!")
else:
print("Okay, thanks for participating. We're glad your experience was", list[1], "!")
return[wordeval,rate]
main()
def main():
[n,l]=用户()
打印(“好的”,n,“from”,l“,让我们开始吧。”)
webname=input(“您希望浏览的网站的名称是什么?”)
website=requests.get(输入(“请输入URL:”))
txt=website.text
list=txt.split(“,”)
打印(键入(txt))
打印(类型(列表))
打印(列表[0:10])
尽管如此:
numkey=input(“您想输入关键字吗?请输入是或否:”)
如果numkey==“是”:
key=input(“请输入要查找的关键字:”)
其他:
newurl()
打破
查找(webname、txt、key)
def查找(web、txt、密钥):
findtext=txt
list=findtext.split(sep=”“)
计数=0
对于列表中的项目:
如果项==键:
计数=计数+1
打印(“单词”,键,“出现”,计数,“时间”,网页)
def newurl():
尽管如此:
新建=输入(“您想浏览其他网站吗?请输入是或否:”)
如果新建==“是”:
main()
其他:
[w,r]=经验()
还新
打破
def user():
name=input(“你好,你叫什么名字?”)
loc=输入(“您来自哪里?”)
返回[姓名,loc]
def experience():
wordeval=input(“请输入3个单词来描述体验,用空格分隔(例如fun cool有趣):”)
list=wordeval.split(sep=”“)
rate=eval(输入(“请从1-10:”)对您的体验进行评分)
如果速率小于6:
打印(“很抱歉您有负面消息”,列表[0],“和”,列表[2],“经验!”)
其他:
打印(“好的,谢谢您的参与。我们很高兴您的体验是”,列表[1],“!”)
返回[wordeval,rate]
main()
您要查找的是re模块。您可以获取匹配项的索引、单个匹配实例等。您可以查看一些关于如何使用模块的好教程,但是逐行遍历html源代码并查找匹配项是很容易的,或者您可以在字符串本身中找到索引(如果您使用换行符拆分它,或者只是将它作为一个长文本字符串保留)。什么是“多个变体”?请定义它,并在find()中提供一个.In,以便能够在源代码中以多种形式将“hello”作为关键字区分开来,例如“hello!”或“hello”。现在它只能以精确的语法检测该词。对此,我深表歉意