Created
December 8, 2011 02:29
-
-
Save zoranzaric/1445842 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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