Get HTTP Error code from requests.exceptions.HTTPError

The HTTPError carries the Response object with it: def get_url_fp(image_url, request_kwargs=None): response = requests.get(some_url, **request_kwargs) response.raise_for_status() return response.raw try: a = “http://example.com” fp = get_url_fp(a) except HTTPError as e: # Need to check its an 404, 503, 500, 403 etc. status_code = e.response.status_code

Using python Requests with javascript pages

Good news: there is now a requests module that supports javascript: https://pypi.org/project/requests-html/ from requests_html import HTMLSession session = HTMLSession() r = session.get(‘http://www.yourjspage.com’) r.html.render() # this call executes the js in the page As a bonus this wraps BeautifulSoup, I think, so you can do things like r.html.find(‘#myElementID’).text which returns the content of the HTML element … Read more

Unit testing a python app that uses the requests library

It is in fact a little strange that the library has a blank page about end-user unit testing, while targeting user-friendliness and ease of use. There’s however an easy-to-use library by Dropbox, unsurprisingly called responses. Here is its intro post. It says they’ve failed to employ httpretty, while stating no reason of the fail, and … Read more

How to send an array using requests.post (Python)? “Value Error: Too many values to unpack”

You want to pass in JSON encoded data. See the API documentation: Remember — All post bodies must be JSON encoded data (no form data). The requests library makes this trivially easy: headers = {“W-Token”: “Ilovemyboss”} data = [ { ‘url’: ‘/rest/shifts’, ‘params’: {‘user_id’: 0, ‘other_stuff’: ‘value’}, ‘method’: ‘post’, }, { ‘url’: ‘/rest/shifts’, ‘params’: {‘user_id’: … Read more

Python: download a file from an FTP server

The requests library doesn’t support ftp:// links. To download a file from an FTP server you could use urlretrieve: import urllib.request urllib.request.urlretrieve(‘ftp://server/path/to/file’, ‘file’) # if you need to pass credentials: # urllib.request.urlretrieve(‘ftp://username:password@server/path/to/file’, ‘file’) Or urlopen: import shutil import urllib.request from contextlib import closing with closing(urllib.request.urlopen(‘ftp://server/path/to/file’)) as r: with open(‘file’, ‘wb’) as f: shutil.copyfileobj(r, f) Python … Read more

Python requests speed up using keep-alive

Yes, there is. Use requests.Session and it will do keep-alive by default. I guess I should include a quick example: import logging import requests logging.basicConfig(level=logging.DEBUG) s = requests.Session() s.get(‘http://httpbin.org/cookies/set/sessioncookie/123456789’) s.get(‘http://httpbin.org/cookies/set/anothercookie/123456789’) r = s.get(“http://httpbin.org/cookies”) print(r.text) You will note that these log message occur INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org DEBUG:requests.packages.urllib3.connectionpool:”GET /cookies/set/sessioncookie/123456789 HTTP/1.1″ 302 223 DEBUG:requests.packages.urllib3.connectionpool:”GET /cookies … Read more

Save a large file using the Python requests library [duplicate]

Oddly enough, requests doesn’t have anything simple for this. You’ll have to iterate over the response and write those chunks to a file: response = requests.get(‘http://www.example.com/image.jpg’, stream=True) # Throw an error for bad status codes response.raise_for_status() with open(‘output.jpg’, ‘wb’) as handle: for block in response.iter_content(1024): handle.write(block) I usually just use urllib.urlretrieve(). It works, but if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)