2015-11-16 49 views
1

該代碼應該要求用戶選擇一個詞來搜索網頁。我認爲最簡單的方法是將整個網頁放在一個列表中,並找出正在搜索的單詞是否在列表中。我有兩個問題,第一個是:我無法將頁面轉換爲列表。第二個問題是我無法獲得正確工作的提示。對不起,我還是很新的python,任何幫助都將不勝感激。將網頁轉換成列表

#p6 scrabble 
#looks for a word in a giant list tells if it is present or not 
words=[] 
import urllib.request 
url='https://www.cs.uoregon.edu/Classes/15F/cis122/data/sowpods_short.txt' 

with urllib.request.urlopen(url) as webpage: #opens the webpage 
    for line in webpage: 
     line= line.strip() 
     line= line.decode('utf-8')#unicode 
     if line [0] != "#": 
      item_list =line.split(',') 
words.append(webpage) 

prompt=input("press L to search for a word or press q to quit") 
while prompt != 'q': 
    question= input("type a word to search for ")  
    if question == words: 
     print("yes, " , "was in the list") 
    elif print("not on the list") 
+0

你不能使用==來一個字比較表(字)或甚至網頁再次 – furas

回答

0
import urllib.request 

words = [] 

url='https://www.cs.uoregon.edu/Classes/15F/cis122/data/sowpods_short.txt' 

with urllib.request.urlopen(url) as webpage: 
    for line in webpage: 
     line = line.strip().decode('utf-8') 
     if line[0] != "#": 
      words += line.split(',') 

print(words) 

while True: 
    question = input("type word or `q` to quit: ") 

    if question == 'q': 
     break 

    if question in words: 
     print("yes,", question, " was on the list") 
    else: 
     print("not on the list") 
+0

感謝,這就是今晚2! –