import io
import sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码

#这将使Selenium WebDriver等待直到完全加载并解析了初始HTML文档,并放弃了样式表,图像和子帧的加载。
#设置为eager时,Selenium WebDriver等待直到 DOMContentLoaded 返回事件
options = Options()
options.page_load_strategy = 'none'
driver = webdriver.Chrome(options=options)


#最大化窗口
driver.maximize_window()
# driver.get("https://www.amazon.com/-/zh/Redragon-S101-Keyboard-Ergonomic-Programmable/dp/B00NLZUM36/ref=sr_1_2?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&crid=13GB0XCVGPJ9P&dchild=1&keywords=keyboard&qid=1597982035&sprefix=keyboa%2Caps%2C328&sr=8-2")
# driver.save_screenshot('./image.png')

# html_source = driver.page_source
# print(html_source)


for num in range(1,14):
    driver.get("https://www.amazon.com/s?k=keyboard&page=%s&qid=1597992544&ref=sr_pg_3" %num)
    html_source = driver.page_source
    print(html_source)

    # driver.save_screenshot('./image.png')

# driver.current_window_handle
# driver.get('https://www.amazon.com')

# html_source = driver.page_source

#采集完成关闭浏览器
driver.close()