Skip to content

Instantly share code, notes, and snippets.

@zoranzaric
Created December 8, 2011 02:29
Show Gist options
  • Save zoranzaric/1445842 to your computer and use it in GitHub Desktop.
Save zoranzaric/1445842 to your computer and use it in GitHub Desktop.
import re
from pyvirtualdisplay import Display
from selenium import webdriver
count_re = re.compile('(\d+) people')
class selenium_firefox():
def __enter__(self):
display, browser = self.setup()
self._display = display
self._browser = browser
return self
def __exit__(self, type, value, traceback):
self.shutdown(self._display, self._browser)
def setup(self):
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
return display, browser
def parse_count(self, url):
self._browser.get(url)
iframe = self._browser.find_element_by_tag_name('iframe')
if iframe is not None:
name = iframe.get_attribute('name')
self._browser.switch_to_frame(name)
matches = count_re.search(self._browser.page_source)
return matches.group(1)
else:
return None
def shutdown(self, display, browser):
browser.quit()
display.stop()
if __name__ == '__main__':
with selenium_firefox() as sf:
print sf.parse_count('http://dump.zoranzaric.de/plus1.html')
print sf.parse_count('http://dump.zoranzaric.de/plus2.html')
print sf.parse_count('http://dump.zoranzaric.de/plus1.html')
print sf.parse_count('http://dump.zoranzaric.de/plus2.html')
print sf.parse_count('http://dump.zoranzaric.de/plus1.html')
print sf.parse_count('http://dump.zoranzaric.de/plus2.html')
print sf.parse_count('http://dump.zoranzaric.de/plus1.html')
print sf.parse_count('http://dump.zoranzaric.de/plus2.html')
print sf.parse_count('http://dump.zoranzaric.de/plus1.html')
print sf.parse_count('http://dump.zoranzaric.de/plus2.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment