Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. def simple_get(url):
  2.     """
  3.    Attempts to get the content at `url` by making an HTTP GET request.
  4.    If the content-type of response is some kind of HTML/XML, return the
  5.    text content, otherwise return None.
  6.    """
  7.     try:
  8.         resp = requests.get(url)
  9.         ctype = resp.headers['Content-Type'].lower()
  10.         if (resp.status_code == 200 and ctype is not None and 'html' in ctype):
  11.             # well-formed response
  12.             return resp.content
  13.     except RequestException as e:
  14.         print(f'Error during requests to {url} : {e}')
  15.  
  16.     return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement