simulate www browser with python

编写于

Simulate www browser with python.

So easy with these libraries:

  • spynner: base on libQtNetwork
  • splinter: base on selenium WebDriver but not stable.

Spynner:


#!/usr/bin/python
# -*- coding: utf-8 -*-

import spynner
import pyquery

browser = spynner.Browser(debug_level=spynner.INFO)
browser.create_webview()
browser.show()
browser.load('http://www.wordreference.com')
browser.load_jquery(True)
browser.choose('input[name=lr=lang_es]')
browser.click('input[name=enit]')
browser.click('a[class=l]:first')
d = pyquery.PyQuery(browser.html)
d.make_links_absolute(base_url=browser.url)
href = d('a:last').attr('href')
print href
print len(browser.download(href))
browser.browse()

Splinter


from splinter.browser import Browser 
browser = Browser() 
# Visit URL 
url = "http://www.google.com"
browser.visit(url) 
browser.fill('q', 'splinter - python acceptance testing for web applications') 
# Find and click the ‘search’ button 
button = browser.find_by_name('btnK') 
# Interact with elements 
button.click() 
if browser.is_text_present('splinter.cobrateam.info'): 
    print "Yes, the official website was found!"
else: 
    print "No, it wasn’t found… We need to improve our SEO techniques"