Simple HTTP GET Request in Python using requests

Including Cython, Jython, IronPython, PyPy, Django framework, and interpreters: Ipython, IPython Jupyter/Notebook, CPython


Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 189
Posts: 5943
Joined: 10 years ago
Location: Tanzania
Contact:

#1

Hey everyone,

Just wanted to share a quick snippet on how to fetch data using an HTTP GET request in Python. It's super straightforward with the requests library.

  1. import requests
  2.  
  3. #URL to read from
  4. url = "YOUR_URL_HERE"  #Replace with your actual URL
  5.  
  6. #Send a GET request
  7. response = requests.get(url)
  8.  
  9. #Check the response status
  10. if response.status_code == 200:
  11.     #Success! Print the content
  12.     print(response.text)
  13. else:
  14.     #Something went wrong
  15.     print(f"Failed to retrieve data: {response.status_code}")

Basically, you import requests, specify your URL, send the GET request, and then check the status_code. A 200 means success, and you can then access the response content (e.g., HTML, JSON) using response.text. Otherwise, the code prints an error message with the status code.

Remember to replace "YOUR_URL_HERE" with the actual URL you want to access! Hope this helps!
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “Python Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest