Python 网页截图

  • Post author:
  • Post category:python


第一步:pip安装selenium

第二步:下载并加载浏览器驱动 chromedriver.exe 这里版本需要匹配

chrome://version/

http://chromedriver.storage.googleapis.com/index.html

第三步:coding

from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
# 无浏览器模式
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

# 设置用户代理
user_agent = (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
        "(KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
)
# user_agent = (
#         "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0"
# )

chrome_options.add_argument('user-agent=%s' % user_agent)

chrome_driver = r"E:\python\chromedriver.exe"

driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)
driver.get("https://www.baidu.com")
driver.maximize_window()
print(driver.get_cookies())
# driver.set_window_position(500,500)
# driver.set_window_rect(300,100,200,200)
sleep(2)
try:
    picture_url=driver.get_screenshot_as_file('1.png')
    print("%s:suces" % picture_url)
except BaseException as msg:
    print(msg)
driver.quit()







版权声明:本文为houxingyao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。