【问题标题】:How to look up functions of a library in python?如何在python中查找库的函数?
【发布时间】:2018-11-07 02:18:22
【问题描述】:

我刚刚安装了这个用于抓取 twitter 数据的库:https://github.com/kennethreitz/twitter-scraper

我想了解库的功能和方法,以便开始与库进行交互。我查看了有关此主题的 StackOverflow 并尝试了以下方法:

  • pydoc twitter_scraper

  • 帮助(twitter_scraper)

  • dir(twitter_scraper)

  • 导入检查并运行函数 = inspect.getmembers(module, inspect.isfunction)

在我尝试过的四件事中,到目前为止,我只得到了检查选项的输出。我也不确定(不包括检查)这些代码应该放在终端还是暂存文件中。

在这方面还是很新的。非常感谢大家阅读!

【问题讨论】:

标签: python documentation-generation inspect pydoc


【解决方案1】:

很好的问题!尝试深入了解(完全理解)一个新库有几个选择。在您的具体情况下,twitter-scraper,唯一的函数是 get-tweets(),整个库不到 80 行。

对于一般情况,按有用性降序排列。

  1. 仔细阅读 GitHub 上的项目描述。 ReadMe 通常是最精心编写的文档。
  2. 较大的库在 http://(package-name).readthedocs.org 提供格式化文档。
  3. pydoc module_name 在安装模块时工作。 ``help(module_name)works in an interactive Python session after you have done animport module_name. These both work from the "docstrings" or strategically placed comments in the source code. This is also whatmodule_name?` 在 iPython 中执行。
  4. dir(module_name) 也需要导入。它列出了模块的所有入口点,包括许多奇怪的“dunder”或双下划线,您通常不会调用或更改。
  5. 阅读源代码。通常,这比文档更容易、更完整。如果您可以在 IDE 中调出代码,那么跳来跳去会很快。

此外,您还询问了可以在脚本中使用的内容:

import os

print("Welcome, human.")
print("dir() is a function, returning a list.")
print("This has no output")
a_list = dir(os)
print("but this does", dir(os))
print("The help() command uses pydoc to print to stdout")
help(os)
print("This program is gratified to be of use.")

【讨论】:

  • 感谢您的广泛回答!我知道 pydoc 和 dir() 都可以通过在脚本中编写代码来访问(而不是终端)。这是正确的吗?
  • 嘿查尔斯。非常感谢你。我现在对如何访问信息有了更好的了解。只是用来更好地理解sklearn库!
【解决方案2】:

这个库似乎缺少适当的文档,但 GitHub 页面提供了一些使用示例来帮助您入门。

>>> from twitter_scraper import get_tweets

>>> for tweet in get_tweets('kennethreitz', pages=1):
>>>     print(tweet['text'])
P.S. your API is a user interface
s3monkey just hit 100 github stars! Thanks, y’all!
I’m not sure what this /dev/fd/5 business is, but it’s driving me up the wall.
…

要获得更多信息,只需查看https://github.com/kennethreitz/twitter-scraper/blob/master/twitter_scraper.py 的源代码。似乎唯一的函数是get_tweets,查看源代码,它接受两个参数,用户名和页数(可选,默认为 25)。

【讨论】:

    猜你喜欢
    • 2014-07-07
    • 2011-03-23
    • 2018-03-18
    • 1970-01-01
    • 2011-04-20
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    相关资源
    最近更新 更多