In addition to getting page lengths and status codes using the request method: Python: Using requests to get web page lengths and status codes You can also use requests to return the source code of web pages. For example: import requests sites = [ ‘http://www.python.org’, ‘http://www.jython.org’, ‘http://www.pypy.org’, ‘http://www.drudgereport.com’, ‘http://www.phys.org’, ‘http://www.bluegalaxy.info’, ‘http://www.bluegalaxy.info/codewalk’ ] for url in … Continue reading Python: Using requests to get web page source text
Python
Python: Using requests to get web page lengths and status codes
With Python 3.6 and the requests module, it is easy to read data from the web. Here are a couple of basic things we can do with the requests module. Getting a Status code import requests r = requests.get(‘http://www.bluegalaxy.info’) print( r.status_code ) Which yields: 200 For more information about HTTP status codes, there are multiple … Continue reading Python: Using requests to get web page lengths and status codes
Python: How to use enumerate( )
Python’s built-in enumerate function takes in any iterable sequence, such as a list, and yields the elements of the sequence along with an index number. This is useful for adding a count to every element of a list. The basic syntax of this function is: enumerate(sequence, start=0) where sequence is the list or iterable data … Continue reading Python: How to use enumerate( )
Python: How to use the dictionary get( ) method
Lets say we have a small Python dictionary called fruit that looks like this: fruit = {‘a’:’apple’, ‘b’:’banana’, ‘c’:’cherry’} The most common way to get a value is to retrieve it by its key, using this special syntax: name_of_dictionary[ name_of_key ] By using the name of the key in brackets after the name of the … Continue reading Python: How to use the dictionary get( ) method
Python: How to use List Comprehensions
List Comprehensions were introduced to the Python language in PEP-202 to provide a more concise syntactical way to produce lists where map(), filter(), for loops, and if statements would normally be used. A list comprehension can be summed up like this: new_list = [item action for item in existing_list with optional if condition] This is … Continue reading Python: How to use List Comprehensions
Flask: How to get up and running in less than 5 minutes
The following instructions are for running Flask on Windows with Python 3 (Anaconda). To run Flask, you will first need to install Flask in your Python Scripts folder. To do this, run `pip install flask` in a command window. Then confirm that the installation was successful by loading python and giving it the import flask command. … Continue reading Flask: How to get up and running in less than 5 minutes
Python: How to use the built-in help( ) and dir( ) functions
In addition to the .__doc__ function which is useful for seeing the docstring of a function or class object, Python has a couple of other useful built-in help functions called help() and dir(). The help() function can be used to inspect functions. For example: >>> help(docstring_test) Help on function docstring_test in module __main__: docstring_test() This … Continue reading Python: How to use the built-in help( ) and dir( ) functions
Python: How to set and read Docstrings
Docstrings are a triple quoted string that you declare at the beginning of object definitions such as functions, classes, and class methods. They are used to describe the purpose of the function, as a brief summary, but not describe how the function works. To describe how a function works, you would use regular comments. A … Continue reading Python: How to set and read Docstrings
Python on the web: How to delete cookies in a CGI script
After you have a cookie that has been set, here is how you can delete it. The trick is to send an update to the cookie where you feed it an expiration date that is already in the past. For example: print “Set-Cookie: xml_edit_tool=Value of cookie; ‘expires=Wed, 28 Aug 2013 18:30:00 GMT’ \r\n” Note: When … Continue reading Python on the web: How to delete cookies in a CGI script
Python on the web: How to update cookies in a CGI script
Once you’ve retrieved a cookie via a function that returns the cookie as a dictionary of key value pairs, you can update the cookie in nearly the same way that you originally set the cookie. The only difference is that this time you can set different values. In this example, I am passing the retrieved … Continue reading Python on the web: How to update cookies in a CGI script