Extract documentation link from Python help() function

Viewed 53

Python has a built-in function called help() which returns the docstrings on methods or functions, per the turtle documentation. For example, say I was to type this:

>>> help('os')

I get greeted with

Help on module os:

NAME
    os - OS routines for NT or Posix depending on what system we're on.

MODULE REFERENCE
    https://docs.python.org/3.8/library/os

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

. . .

and so forth. It is quite simple to find the module reference by just looking at this, but say I was collecting the references to 100 different modules. It would take quite some time and would be very repetitive work.

How can I parse through each help() function for the link to the module's documentation? It would involve finding a similar value such as https:// or .org or .com.

1 Answers

I'd argue that you actually don't need to do any parsing, since as far as I know, all the standard library Python modules have documentation accessible at the URL https://docs.python.org/<version>/library/<modulename>. It would be far more efficient to construct the URL according to that pattern compared to parsing the help text.

That being said, if you really do want to parse the help text, the re.search function should be useful. You can write a regular expression to match the URL of a Python standard library documentation page and presumably the first match should be the result you want.

Related