将网页转换为列表

时间:2015-11-16 03:09:10

标签: python html list python-3.x

此代码应该要求用户选择一个单词来搜索网页。我认为最简单的方法就是将整个网页放到列表中,找出被搜索的单词是否在列表中。我有2个问题,第一个是:我无法将页面转换为列表。第二个问题是我无法获得正常工作的提示。对不起,我对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")

1 个答案:

答案 0 :(得分: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")