Grabbing a Page Title with Python for Basic Enumeration

Here’s a quick writeup for how to grab a page title with Python.

Grabbing a Page Title with Python – Introduction

This will be another shorter post, as I have some last-minute work travel coming up.

I promise that there are more vulnserver posts coming, I just need some time to finish them.

Other than that, I was on an engagement recently and wanted to grab the title for a huge list of webpages. Note that the http-title nmap script works great, but I didn’t have nmap on the box that I was using.

Idea, Implementation, and Execution

As the introduction mentioned, I was wanting to grab the title for a list of web pages using Python. Similar to some of my previous tools, I decided to use BeautifulSoup for this.

This was far easier than I expected, and I could probably build it out into a real web enumeration tool.

C:\Users\doyler\Documents>py grabTitle.py

Page URL and Title
-----------------------------------------------------------------
http://www.google.com = Google
http://www.cnn.com = CNN - Breaking News, Latest News and Videos

The Code

You can find the entire script below, and it is fairly straightforward. It will connect to a URL using the requests library, and then parse out the title using BeautifulSoup.

This only supports a list of URLs for now, but could easily be extended to support an input file of URLs/IPs.

import requests
from bs4 import BeautifulSoup

def main():
	print("\nPage URL and Title")
	print("-----------------------------------------------------------------")
	
	urls = ['http://www.google.com', 'http://www.cnn.com']
	
	for url in urls:
		r = requests.get(url)
		soup = BeautifulSoup(r.text, 'html.parser')
		
		print(url + " = " + soup.title.string)
		
if __name__ == "__main__":
    main()

Grabbing a Page Title with Python – Conclusion

I know this was a shorter post than normal, but I’m still trying to catch up on everything.

Let me know if you would like to see any real features added to this script. If so, then I can probably update it and turn it into a useful enumeration tool.

Finally, you can find the code and updates in my GitHub repository.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.