Posts

Showing posts from 2014

Python Mechanize Cheat Sheet

Image
Mechanize Create a browser object Create a browser object and give it some optional settings. import mechanize br = mechanize.Browser() br.set_all_readonly(False)    # allow everything to be written to br.set_handle_robots(False)   # ignore robots br.set_handle_refresh(False)  # can sometimes hang without this br.addheaders =         # [('User-agent', 'Firefox')] Open a webpage Open a webpage and inspect its contents response = br.open(url) print response.read()      # the text of the page response1 = br.response()  # get the response again print response1.read()     # can apply lxml.html.fromstring() Using forms List the forms that are in the page for form in br.forms():     print "Form name:", form.name     print form To go on the mechanize browser object must have a form selected br.select_form("form1")         # works when form has a name br.form = list(br.forms())[0]  # use when form is unnamed Using Contro

Python mechanize For Browsing

Image
How to install Mechanize we can install mechanize in two ways  Using pip : pip install mechanize Or download the mechanize distribution,open it and run it: python setup.py install  Browsing with Mechanize  Here is an example on how to browse webpage in python program   import mechanize   br = mechanize.Browser()   br.open("http://www.example.com/") Follow second link with element text matching regular expression response1 = br.follow_link(text_regex=r"cheese\s*shop",nr=1) assert br.viewing_html()   print br.title()   print response1.geturl()   print response1.info() # headers   print response1.read() # body To get the response code from a website, you can the response.code   from mechanize import Browser   browser = Browser()   response = browser.open('http://www.google.com')   print response.code Get all forms from a website   import mechanize   br = mechanize.Browser() br.open("http://www.google.com/")  

Rich text editor in Django

Image
M any people often want What You See Is What You Get (WYSIWYG) editors when creating content from within their web applications. This can be either in the Django admin control panel or for the end user. One of the most flexible and useful of the WYSIWYG editors currently available is TinyMCE due to its extensive plugin library and robust feature set. Integration with Django is relatively simple given that you extend the functionality of built in classes. The first thing we will need to do is create a custom widget from forms.Textarea found in the django.forms module. We accomplish this by inheriting the Textarea class and overriding the constructor and render methods. Defining the relative path (on your web server) to the TInyMCE javascript source is also required here. So be aware that you will need to change that path to suite your environment. You may also want to change the content_css variable to include your sites’ main CSS file. The following is the source code for widgets.py