Requests library

The requests library simplifies the process of making HTTP requests to interact with web services, APIs, or websites

  • importing request library
  • Making get request and handling response
  • Using request.post
  • working the parameters
  • handling errors
import requests


Request

  • The requests module allows you to send HTTP requests using Python.
  • In order to download requests, you would have to type pip install requests in your terminal.

Syntax

  • requests.methodname(params)
import requests

x = requests.get('https://w3schools.com/python/demopage.htm')

print(x.text)
<!DOCTYPE html>
<html>
<body>

<h1>This is a Test Page</h1>

</body>
</html>

Pillow

  • Pillow is a imaging library that provides easy-to-use methods to include, change, save different image formats.
  • To dowload pillow onto your computer, you would enter the command pip install Pillow into your terminal.
from PIL import Image

# Open an image
original_image = Image.open('image.png')

# Display information about the image
width, height = original_image.size
format = original_image.format
print(f"Original Image Size: {width}x{height}")
print(f"Original Image Format: {format}")

# Resize the image to a new size
new_size = (width // 2, height // 2)  # Reduce the size by half
resized_image = original_image.resize(new_size)

# Save the resized image
resized_image.save('resized_image.jpg')

# Display information about the resized image
resized_width, resized_height = resized_image.size
print(f"Resized Image Size: {resized_width}x{resized_height}")

# Show both the original and resized images
original_image.show()
resized_image.show()

Original Image Size: 713x1099
Original Image Format: PNG
Resized Image Size: 356x549


WARNING: You don't seem to have any mimeinfo.cache files.
Try running the update-desktop-database command. If you
don't have this command you should install the
desktop-file-utils package. This package is available from
http://freedesktop.org/wiki/Software/desktop-file-utils/
WARNING: You don't seem to have any mimeinfo.cache files.
Try running the update-desktop-database command. If you
don't have this command you should install the
desktop-file-utils package. This package is available from
http://freedesktop.org/wiki/Software/desktop-file-utils/
No applications found for mimetype: image/png
.No applications found for mimetype: image/png
./bin/xdg-open: 882: x-www-browser: not found
/bin/xdg-open: 882: x-www-browser: not found
/bin/xdg-open: 882: firefox: not found
/bin/xdg-open: 882: firefox: not found
/bin/xdg-open: 882: iceweasel: not found
/bin/xdg-open: 882: iceweasel: not found
/bin/xdg-open: 882: seamonkey: not found
/bin/xdg-open: 882: seamonkey: not found
/bin/xdg-open: 882: mozilla: not found
/bin/xdg-open: 882: mozilla: not found
/bin/xdg-open: 882: epiphany: not found
/bin/xdg-open: 882: epiphany: not found
/bin/xdg-open: 882: konqueror: not found
/bin/xdg-open: 882: konqueror: not found
/bin/xdg-open: 882: chromium: not found
/bin/xdg-open: 882: chromium: not found
/bin/xdg-open: 882: chromium-browser: not found
/bin/xdg-open: 882: chromium-browser: not found
/bin/xdg-open: 882: google-chrome: not found
/bin/xdg-open: 882: google-chrome: not found
/bin/xdg-open: 882: www-browser: not found
/bin/xdg-open: 882: www-browser: not found
/bin/xdg-open: 882: links2: not found
/bin/xdg-open: 882: links2: not found
/bin/xdg-open: 882: elinks: not found
/bin/xdg-open: 882: elinks: not found
/bin/xdg-open: 882: links: not found
/bin/xdg-open: 882: links: not found
/bin/xdg-open: 882: lynx: not found
/bin/xdg-open: 882: lynx: not found
/bin/xdg-open: 882: w3m: not found
xdg-open: no method available for opening '/tmp/tmpercgw3v3.PNG'
/bin/xdg-open: 882: w3m: not found
xdg-open: no method available for opening '/tmp/tmpgy4_rh_f.PNG'

Reflection:
Libraries and Requests:

  • Libraries Are Like Toolkits: I learned that libraries are like toolkits that provide pre-written code for common tasks, saving time and effort.

  • Importing Libraries: Importing libraries into my code is straightforward, and it extends the functionality of my programs.

  • Common Libraries: I explored popular libraries like NumPy for numerical operations, Pandas for data manipulation, and Matplotlib for data visualization.

  • Requests for Web Data: I discovered how to use libraries like Requests in Python to fetch data from websites, which is crucial for web scraping and API interactions.

  • Handling Responses: I learned how to send requests to web servers and handle responses, extracting and processing data for various applications.

  • API Interaction: Understanding APIs allowed me to access data from services like weather forecasts, social media platforms, and more, enhancing my application’s capabilities.

  • Error Handling: Dealing with errors in library usage became an essential skill, ensuring robust and reliable programs.

  • Efficiency and Productivity: Using libraries and making requests improved my coding efficiency and productivity, enabling me to achieve more with less effort.

  • Documentation: Reading and understanding library documentation became crucial for effectively using libraries and making requests.

  • In summary, libraries and requests are like handy toolkits that make programming more efficient and versatile. They extend the capabilities of my code, whether I’m working with data, accessing web resources, or performing various tasks. Understanding how to use them is a valuable skill in computer science.