In Python, you can use the requests
library to make HTTP GET requests to retrieve data from a web server. Here’s a basic example of how to use requests.get()
:
import requests
# Replace this URL with the one you want to request data from
url = "https://example.com"
# Send a GET request to the specified URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Print the content of the response (usually HTML or JSON data)
print(response.text)
else:
# If the request was not successful, print an error message
print(f"Request failed with status code {response.status_code}")
In this example:
- Import the
requests
library. - Define the URL you want to request data from.
- Use
requests.get(url)
to send a GET request to the specified URL. - Check the response’s status code to ensure the request was successful (status code 200 indicates success).
- If the request was successful, you can access the content of the response using
response.text
and process it as needed.
Make sure you have the requests
library installed in your Python environment. You can install it using pip if you haven’t already:
pip install requests
Remember to replace "https://example.com"
with the actual URL you want to request data from.