Requesting a URL from a local File in Python
Last Updated :
28 Apr, 2025
Making requests over the internet is a common operation performed by most automated web applications. Whether a web scraper or a visitor tracker, such operations are performed by any program that makes requests over the internet. In this article, you will learn how to request a URL from a local File using Python.
Requesting a URL from a local File in the Python
webbrowser library would be used to include URL displaying capabilities to the python interpreter. The library could be installed using the following command:
py -m pip install webbrowser
The following file containing URLs on each line will be used for demonstration.
a file containing 3 URLs on separate linesExample 1: Requesting a URL from a local File using webbrowser
The aforementioned file would be read, and all its contents would be stored (delimited by a newline character) as members of a list where each line would contain a URL. Next, a loop would run to go over each of these URLs. In each iteration, a request to the resource located at the particular URL would be made using the webbrowser.open function. This opens the default web browser of the Operating System to display the URL.
Python3
import webbrowser
# Opening the file containing URLs in each line
# Extracting the URL from each line and storing it in a list
url_list = open('test.txt').readlines()
# This loop iterates over all the URLs present in the file
for url in url_list:
# Opening the webbrowser of the OS and displaying the URL
webbrowser.open(url)
Output:
Firstly the file containing the URLs is opened (in read mode), and all the lines in the file are stored as separate elements of a list. A loop is run on the list, which iterates over all its elements, where the elements are URLs. In each iteration, a request to the specific URL is made, and the response is displayed using the default web browser of the operating system.
The file containing the URLs needs to contain a URL on each line. Otherwise, the code must be modified to reflect such changes in the input file.
Method 2: Requesting a URL from a local File using requests
Another way of accomplishing the task at hand, without the overhead of running a browser, is by requesting the webpage and storing the response. This allows several operations that could be performed on the data obtained from the URL while not requiring the display of its contents. In the following example, the use of request module would be made to produce the desired effect.
Python3
import requests
# Opening the file containing URLs in each line
# Extracting the URL from each line and storing it in a list
url_list = open('test.txt').readlines()
# This loop iterates over all the URLs present in the file
for url in url_list:
# Making the request for the URL and storing its response
resp = requests.get(url)
# Displaying the response code & the content of the response
print(resp)
print(resp.content)
Output:
The process of extracting URLs from the file is the same as in the previous example. The only difference lies in the loop body. Inside the loop, the get function makes a request to the URL, and the response is stored in a variable. This variable could be used to obtain a lot of information regarding the URL. Firstly, the response code is displayed, which in our case is 200 (success status response code). After which the contents of the webpage received in response are displayed. If this content is stored within a file with an appropriate extension, it will act as an offline copy of the URL.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read