简体   繁体   中英

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

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:

  • pydoc twitter_scraper

  • help(twitter_scraper)

  • dir(twitter_scraper)

  • imported inspect and ran functions = 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.

For the general case, in decreasing order of usefulness.

  1. Carefully read the project's description on GitHub. The ReadMe is usually the most carefully written piece of documentation.
  2. Larger libraries have formatted documentation at http://(package-name).readthedocs.org .
  3. pydoc module_name works when the module is installed. ``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 . 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.
  4. dir(module_name) also requires an import. It lists all the entrypoints to the module, including lots of odd "dunder", or double underscore, you would not normally call or change.
  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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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