import requests response = requests.get('https://api.example.com/data') # replace with actual API URL data = response.json() # parse the response JSON to a Python object print(data)
import requests response = requests.get('https://www.example.com') # replace with actual webpage URL html = response.text # get the HTML content as a string print(html)
import requests image_url = 'https://example.com/image.jpg' # replace with actual image URL response = requests.get(image_url) with open('image.jpg', 'wb') as f: f.write(response.content)In all of these examples, we import the `requests` library and use the `get()` function to make a request to a remote server. The `get()` function takes a URL as its argument and returns a `Response` object, which contains information about the server's response (such as status code, headers, and content). To work with the content of the response, we can use various methods on the `Response` object. For example, `response.json()` parses the response content as JSON and returns a Python object, while `response.text` returns the response content as a Unicode string. Overall, the `get()` function in Python can be a powerful tool for making HTTP requests and retrieving data from remote servers.