2

I currently find myself using the bs4/BeautifulSoup library a lot in python, and have recently been wondering how it works. I would love to see the source code for the library and don't know how. Does anyone know how to do this?

3 Answers 3

5

The first step is to ask the module itself. Usually its all you need

>>> import bs4
>>> bs4.__file__
'/home/td/anaconda3/lib/python3.7/site-packages/bs4/__init__.py'
>>> exit()
$ pushd /home/td/anaconda3/lib/python3.7/site-packages/bs4
$ ls
builder    diagnose.py  __init__.py  testing.py
dammit.py  element.py   __pycache__  tests
Sign up to request clarification or add additional context in comments.

4 Comments

I admit I don't know Python, but where is the source code shown in this answer? It looks to me like it is just showing files/libraries used by the library BeautifulSoup, not showing the actual source code.
@TylerH - No, that's the source. When you import bs4, the __init__.py file referenced here is loaded and executed (or it could be the compiled version in __pycache__). If you want to learn BS from the source, its a great place to start. That file is 800+ lines, so I don't show it or the other .py files in the answer, but I did show an easy way for you to get it yourself.
Thanks, though it sounds like my concern is borne out by your explanation. A list of constituent files called by the script is not really "the source code". The answer is useful because it shows how to find the filepaths of the source code, but does not show how to get the source code itself, which is ultimately what the question asked. This answer could be improved by showing code needed to output (or open?) the actual source code of the various constituent files that pack up the bs4 package. Listening the contents of that code in the answer is, of course, not required.
@TylerH - OP asked how BeautifulSoup works and I showed a technique generally useful for packages written in python. I'm not going to show how to open the .py files, that's very opinion based. That's something you're going to have to figure out on your own. If you want to know about the packaging, then you may need to dig up the source repo. But that's not what as asked here.
1

If you are using any IDE, you can right click on imported line and goto Implementation. Otherwise you can find the source code in <python_installtion_path>\Lib\site-packages directory.

Comments

0

Go to the location where python is installed and inside the python folder, you will have a folder called Lib you can find all the packages there open the required python file you will get the code.

example location: C:\Python38\Lib

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.