2 issues when getting Yahoo Finance historical data in Selenium python? -
currently try download historical stock prices yahoo finance personal research purpose. when used selenium in python download data, encountered 2 issues: 1. took long time download web page because has lot of external links need load. there loading timeout exception.
- when used try , exception deal timeout exception, button used change date doesn't work. guess caused web page hasn't been totally loaded.
i beginner python , selenium, please advise on issue?
find below 3 methods:
checking page readystate (not reliable):
def page_has_loaded(self): self.log.info("checking if {} page loaded.".format(self.driver.current_url)) page_state = self.driver.execute_script('return document.readystate;') return page_state == 'complete' comparing new page ids old one:
def page_has_loaded2(): self.log.info("checking if {} page loaded.".format(self.driver.current_url)) try: new_page = browser.find_element_by_tag_name('html') return new_page.id != old_page.id except nosuchelementexception: return false using staleness_of method:
@contextlib.contextmanager def wait_for_page_load(self, timeout=10): self.log.debug("waiting page load @ {}.".format(self.driver.current_url)) old_page = self.find_element_by_tag_name('html') yield webdriverwait(self, timeout).until(staleness_of(old_page)) for more details, check harry's blog.
hope :)
Comments
Post a Comment