python 连接到网络和验证用户输入的基础知识

Basics of connecting python to the web and validating user input

我是新手,不知道从哪里开始。我不希望有详细的逐步响应(当然,我们非常欢迎这些响应),但我们将不胜感激任何朝着正确方向的推动。

我想使用 Gutenberg python 库 select 基于用户输入的文本。

现在我有代码:

from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers

text = strip_headers(load_etext(11)).strip()

其中数字代表文本(在本例中 11 = 爱丽丝梦游仙境)。

然后我有一堆关于如何处理文本的代码,但我认为这与这里无关。 (如果是请告诉我,我可以添加)。

基本上,我想让用户这样做,而不仅仅是 select输入文本。我想询问用户对作者的选择,如果 Project Gutenberg (PG) 有该作者的作品,则从书名列表中选择 select(如果 PG 没有该作者的任何作品) , return 一些类似 "sorry, don't have anything by $author_name, pick someone else." 的响应 然后,一旦用户决定购买一本书,就将与该书对应的编号输入代码中。

我只是不知道在这个过程中从哪里开始。我知道如何处理用户输入,但我不知道如何获取该输入并使用它在线搜索内容。

理想情况下,我也能够处理诸如拼写错误之类的问题,但这可能不太可行。

我非常感谢任何人有时间提供的帮助。谢谢!

gutenberg 模块包含 searching for a text by metadata 的工具,例如 author。文档中的示例是:

from gutenberg.query import get_etexts
from gutenberg.query import get_metadata

print(get_metadata('title', 2701))  # prints frozenset([u'Moby Dick; Or, The Whale'])
print(get_metadata('author', 2701)) # prints frozenset([u'Melville, Hermann'])

print(get_etexts('title', 'Moby Dick; Or, The Whale'))  # prints frozenset([2701, ...])
print(get_etexts('author', 'Melville, Hermann'))        # prints frozenset([2701, ...])

听起来好像您已经知道如何将用户的值读入变量,并且替换上面的文字作者就像做这样的事情一样简单:

author_name = my_get_input_from_user_function()
texts = get_etexts('author', author_name)

请注意同一部分的以下注释:

Before you use one of the gutenberg.query functions you must populate the local metadata cache. This one-off process will take quite a while to complete (18 hours on my machine) but once it is done, any subsequent calls to get_etexts or get_metadata will be very fast. If you fail to populate the cache, the calls will raise an exception.

考虑到这一点,我还没有尝试我在这个答案中提供的代码,因为我仍在等待我的本地缓存填充。