Fetching URL Content with urllib3#s PoolManager

  • Share this:

Code introduction


This function uses urllib3's PoolManager to send a GET request and fetch the content from a URL. It handles potential HTTP errors.


Technology Stack : urllib3, PoolManager, HTTPError

Code Type : Function

Code Difficulty : Intermediate


                
                    
def fetch_url_content(url, timeout=10):
    """
    Fetch the content of a URL using urllib3's PoolManager.
    """
    from urllib3 import PoolManager, HTTPError
    try:
        with PoolManager() as http:
            response = http.request('GET', url, timeout=timeout)
            return response.data
    except HTTPError as e:
        print(f"HTTP error occurred: {e}")
        return None

# JSON representation of the code