python--selenium--PhantomJS--mac版本(网页截图)

1 python安装selenium包

# pip install selenium==3.11.0 -i https://pypi.doubanio.com/simple

2 PhantomJS安装

# wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-macosx.zip
# unzip phantomjs-2.1.1-macosx.zip
# cp phantomjs-2.1.1-macosx/bin/phantomjs /usr/local/bin/

3 配置hosts

127.0.0.1 localhost

4 截图代码

# -*- coding:utf-8 -*-
 
from selenium import webdriver
import time
 
 
def make_screenshot(url, file_path, time_sleep=3):
    driver = webdriver.PhantomJS()
    driver.maximize_window()
 
    driver.get(url)
    
    #模拟登录
    # driver.find_element_by_id('id_username').send_keys('[email protected]')
    # driver.find_element_by_id('id_password').send_keys('pwdxxxxx')
    # driver.find_element_by_css_selector('button.btn-primary').click()
 
    time.sleep(time_sleep)
 
    width = driver.execute_script("return document.documentElement.scrollWidth")
    height = driver.execute_script("return document.body.scrollHeight")
 
    driver.set_window_size(width, height)
 
    driver.save_screenshot(file_path)
 
    driver.quit()
 
 
def push_bot_image(file_path, web_hook):
    """
 
    :return:
    """
    import base64
    f = open(file_path, 'rb')
    file_read = f.read()
    base64_data = base64.b64encode(file_read)
    png_base64 = base64_data.decode()
 
    from hashlib import md5
    my_md5 = md5()
    my_md5.update(file_read)
    png_md5 = my_md5.hexdigest()
 
    f.close()
 
    import requests, json
 
    headers = {'Content-Type': 'application/json'}
    data = {
        "msgtype": "image",
        "image": {
            "base64": png_base64,
            "md5": png_md5
        }
    }
 
    requests.post(url=web_hook, headers=headers, data=json.dumps(data), timeout=5)
 
 
if __name__ == "__main__":
    url = 'http://sentry.net/hero/mail/'
    # url = 'https://www.baidu.com'
    pic_name = r'/Users/clevercode/Downloads/appscore.png'
    make_screenshot(url, pic_name)
 
    web_hook = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c05bffa7-xxxxx-8965-7810aaef9ceb'
    #企业微信机器人消息
    push_bot_image(pic_name, web_hook)

技术交流

CleverCode是一名架构师,技术交流,咨询问题,请加CleverCode创建的qq群(架构师俱乐部):517133582。加群和腾讯,阿里,百度,新浪等公司的架构师交流。【架构师俱乐部】宗旨:帮助你成长为架构师!
在这里插入图片描述

你可能感兴趣的:(架构师)