Let’s say that you would like to get all the links from a website, how would you achieve that? Today, we’ll be using python to extract every link from the dev.to homepage.
Step 1: Install Pip3
If you have pip3 already installed, feel free to skip this part.
To check if you have installed pip3, open up the command prompt for windows, UNIX command shell for Linux or terminal for mac. After that, run the following command.
If it returns with something that looks like this you are good to go:
Usage: C:\Python38\python.exe -m pip <command> [options] Commands: install Install packages. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. ...
Otherwise, read this guide to install pip3:
pip3 installation instructions
Step 2: Install Dependencies
If you already have requests and bs4 installed, feel free to skip this step, otherwise, open the command prompt(Terminal on mac) and run the following commands.
pip3 install bs4
pip3 install requests
Step 3: Coding
Open your IDE of choice, I’ll be using Visual studio code, which you can download below:
Visual studio code download
Now create a folder anywhere on your laptop, open the folder on your IDE(file>open folder on vscode), then create a file in the folder. Name the file scrape.py.
After that, paste this bunch of code into the python file.
import requests from bs4 import BeautifulSoup def listToString(s): # initialize an empty string str1 = "" # traverse in the string for ele in s: str1 += ele+'\n' # return string return str1 links =  url = "https://dev.to" website = requests.get(url) website_text = website.text soup = BeautifulSoup(website_text) for link in soup.find_all('a'): links.append(link.get('href')) number = 0 for link in links: print(link) print(len(links))
Basically, you are using requests to get the HTML document from the URL you provided, then you are getting all the anchor tags(links) from the website and printing them out.
Now if you run the file(run>start debugging on vs code) you should see quite a few links being printed out, followed by the total number of links included.
I hope this worked for you, It took me a bit of trial and error to get this working, please comment below if it did not work for you, and I’ll be in touch within 3 business days.
Anyway, thanks a lot and have a good day.