如何在 python 中查找库的函数?

How to look up functions of a library in python?

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

我想找出图书馆的功能和方法,以便开始与图书馆互动。我查看了关于这个主题的 Whosebug 并尝试了以下方法:

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

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

这个库似乎缺少适当的文档,但是 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)。

问得好!尝试理解(完全理解)一个新库有几种选择。在您的特定情况下,twitter-scraper,唯一的功能是 get-tweets(),整个库不到 80 行。

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

  1. 仔细阅读 GitHub 上的项目描述。 ReadMe 通常是最仔细编写的文档。
  2. 较大的图书馆在 http://(package-name).readthedocs.org.
  3. 处有格式化文档
  4. 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.
  5. 中执行
  6. dir(module_name) 也需要导入。它列出了模块的所有入口点,包括许多奇怪的 "dunder" 或双下划线,您通常不会调用或更改。
  7. 阅读源代码。通常,这比文档更容易、更完整。如果您可以在 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.")