GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — How to Extract All Website Links in Python - The Python Code Turn your code into any language with our Code Converter. It's the ultimate tool for multi-language programming. Start converting now! Extracting all links of a web page is a common task among web scrapers. It is useful to build advanced scrapers that crawl every page of a certain website to extract data. It can also be used for the SEO diagnostics process or even the information gathering phase for penetration testers. In this tutorial, you will learn how to build a link extractor tool in Python from Scratch using only requests and BeautifulSoup libraries. Note that there are a lot of link extractors out there, such as Link Extractor by Sitechecker. The goal of this tutorial is to build one on your own using Python programming language. Get: Ethical Hacking with Python EBook Let's install the dependencies:
We'll be using requests to make HTTP requests conveniently, BeautifulSoup for parsing HTML, and colorama for changing text color. Open up a new Python file and follow along. Let's import the modules we need:
We are going to use colorama just for using different colors when printing to distinguish between internal and external links:
We gonna need two global variables, one for all internal links of the website and the other for all the external links:
Since not all links in anchor tags (
This will ensure that a proper scheme (protocol, e.g http or https) and domain name exist in the URL. Now, let's build a function to return all the valid URLs of a web page:
First, I initialized the Second, I've extracted the domain name from the URL. We gonna need it to check whether the link we grabbed is external or internal. Third, I've downloaded the HTML content of the web page and wrapped it with a Let's get all HTML
So we get the href attribute and check if there is something there. Otherwise, we just continue to the next link. Since not all links are absolute, we gonna need to join relative URLs with their domain name (e.g when the href is "/search" and the url is "google.com", the result will be "google.com/search"):
Now, we need to remove HTTP GET parameters from the URLs since this will cause redundancy in the set. The below code handles that:
Let's finish up the function:
Related: Ethical Hacking with Python EBook All we did here was checking:
Finally, after all checks, the URL will be an internal link. We print it and add it to our The above function will only grab the links of one specific page; what if we want to extract all the links for the entire website? Let's do this:
This function crawls the website, which means it gets all the links of the first page and then calls itself recursively to follow all the links extracted previously. However, this can cause some issues; the program will get stuck on large websites (that have many links), such as Alright, let's test this; make sure you use this on a website you're authorized to. Otherwise, I'm not responsible for any harm you cause.
Get our Ethical Hacking with Python EBook I'm testing it on our website. However, I highly encourage you not to do that; that will cause a lot of requests as it will crowd our little web server and block your IP address. Here is a part of the output: After the crawling finishes, it'll print the total links extracted and crawled:
Awesome, right? I hope this tutorial was a benefit for you and inspired you to build such tools using Python. Some websites load most of their content using JavaScript. Therefore, we need to use the requests_html library instead, which enables us to execute Javascript using Chromium; I already wrote a script for that by adding just a few lines (as requests_html is quite similar to requests). Check it out here. Requesting the same website many times in a short period may cause the website to block your IP address. In that case, you need to use a proxy server for such purposes. If you're interested in grabbing images instead, check this tutorial: How to Download All Images from a Web Page in Python, or if you want to extract HTML tables, check this tutorial. I edited the code a little bit so you can save the output URLs in a file and pass URLs from command line arguments. I highly suggest you check the complete code here. In our Ethical Hacking with Python EBook, we have used this code to build an advanced email spider that goes into every link extracted and searches for email addresses. Make sure to check it out here! Happy Scraping ♥ Save time and energy with our Python Code Generator. Why start from scratch when you can generate? Give it a try!
Sharing is caring!
Read alsoHow to Download All Images from a Web Page in PythonLearn how to extract and download images from a single web page in Python using requests and BeautifulSoup libraries. How to Extract and Submit Web Forms from a URL using PythonLearn how you can scrape forms from web pages, as well as filling and submitting them using requests html and beautiful soup in Python. How to Extract Script and CSS Files from Web Pages in PythonBuilding a tool to extract all Javascript and CSS files from a web page in Python using requests and BeautifulSoup. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |