简体   繁体   English

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

[英]How to look up functions of a library in python?

I just installed this library that scrapes twitter data: https://github.com/kennethreitz/twitter-scraper我刚刚安装了这个抓取 twitter 数据的库: https : //github.com/kennethreitz/twitter-scraper

I wanted to find out the library's functions and methods so I can start interacting with the library.我想找出库的函数和方法,以便我可以开始与库进行交互。 I have looked around StackOverflow on this topic and tried the following:我在这个主题上环顾了 StackOverflow 并尝试了以下操作:

  • pydoc twitter_scraper pydoc twitter_scraper

  • help(twitter_scraper)帮助(twitter_scraper)

  • dir(twitter_scraper)目录(twitter_scraper)

  • imported inspect and ran functions = inspect.getmembers(module, inspect.isfunction)导入的检查和运行函数 = inspect.getmembers(module, inspect.isfunction)

Of the four things I have tried, I have only gotten an output from the inspect option so far.在我尝试过的四件事中,到目前为止,我只从检查选项中获得了输出。 I am also unsure (excluding inspect) whether these codes should go in the terminal or a scratch file.我也不确定(不包括检查)这些代码应该放在终端还是临时文件中。

Still quite new at this.在这方面还是很新的。 Thank you so much for reading everybody!非常感谢大家阅读!

Excellent question!好问题! There are a few options in trying to grok (fully understand) a new library.尝试理解(完全理解)一个新库有几种选择。 In your specific case, twitter-scraper , the only function is get-tweets() and the entire library is under 80 lines long.在您的特定情况下, twitter-scraper ,唯一的功能是get-tweets()并且整个库不到 80 行。

For the general case, in decreasing order of usefulness.对于一般情况,按有用性降序排列。

  1. Carefully read the project's description on GitHub.仔细阅读 GitHub 上的项目描述。 The ReadMe is usually the most carefully written piece of documentation.自述文件通常是编写最仔细的文档。
  2. Larger libraries have formatted documentation at http://(package-name).readthedocs.org .较大的库已在http://(package-name).readthedocs.org 上格式化文档。
  3. pydoc module_name works when the module is installed. pydoc module_name在安装模块时起作用。 ``help(module_name) works in an interactive Python session after you have done an import module_name . These both work from the "docstrings" or strategically placed comments in the source code. This is also what ``help(module_name) works in an interactive Python session after you have done an导入 module_name works in an interactive Python session after you have done an . These both work from the "docstrings" or strategically placed comments in the source code. This is also what . These both work from the "docstrings" or strategically placed comments in the source code. This is also what . These both work from the "docstrings" or strategically placed comments in the source code. This is also what module_name?` does in iPython. . These both work from the "docstrings" or strategically placed comments in the source code. This is also what module_name?` 在 iPython 中所做的。
  4. dir(module_name) also requires an import. dir(module_name)也需要导入。 It lists all the entrypoints to the module, including lots of odd "dunder", or double underscore, you would not normally call or change.它列出了模块的所有入口点,包括许多奇怪的“dunder”或双下划线,您通常不会调用或更改。
  5. Read the source code.阅读源代码。 Often, this is easier and more complete than the documentation.通常,这比文档更容易、更完整。 If you can bring up the code in an IDE, then jumping around works quickly.如果您可以在 IDE 中调出代码,则可以快速跳转。

Also, you asked about what can be used within a script:此外,您询问了可以在脚本中使用的内容:

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.")

It seems like this library lacks proper documentation, but the GitHub page provides some usage examples to help you get started.这个库似乎缺乏适当的文档,但 GitHub 页面提供了一些使用示例来帮助您入门。

 >>> from twitter_scraper import get_tweets >>> for tweet in get_tweets('kennethreitz', pages=1): >>> print(tweet['text']) PS 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. …

To get more information, simply look at the source code at https://github.com/kennethreitz/twitter-scraper/blob/master/twitter_scraper.py .要获取更多信息,只需查看https://github.com/kennethreitz/twitter-scraper/blob/master/twitter_scraper.py上的源代码。 It seems like the only function is get_tweets , which, looking at the source code, takes in two arguments, the username and the number of pages (optional, defaults to 25).似乎唯一的函数是get_tweets ,查看源代码,它接受两个参数,用户名和页数(可选,默认为 25)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM