首页 > 代码库 > 浏览器翻页

浏览器翻页

from selenium import webdriver
import time


def capture(url, save_fn="capture.png"):
    browser = webdriver.Ie()  # Get local session of firefox

    browser.get(url)  # Load page
    browser.execute_script("""
    (function () {
      var y = 0;
      var step = 100;
      window.scroll(0, 0);

      function f() {
        if (y < document.body.scrollHeight) {
          y += step;
          window.scroll(0, y);
          setTimeout(f, 50);
        } else {
          window.scroll(0, 0);
          document.title += "scroll-done";
        }
      }

      setTimeout(f, 1000);
    })();
  """)

    for i in range(30):
        if "scroll-done" in browser.title:
            break
        time.sleep(1)

    browser.save_screenshot(save_fn)
    browser.close()


if __name__ == "__main__":
    capture("https://item.taobao.com/item.htm?id=555415290067&ali_trackid=2:mm_121521933_21312413_71796115:1500716844_310_1710961526&upsid=023bcc6ceedd18a7da7cb5fd1ff7252e&clk1=023bcc6ceedd18a7da7cb5fd1ff7252e")

 

浏览器翻页