python实现自动网页截图并裁剪图片

yipeiwu_com5年前Python基础

本文实例为大家分享了python自动网页截图并裁剪图片的具体代码,供大家参考,具体内容如下

代码:

# coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image
import os

all_urls = ['http:/****edit']
def login():
  chrome_options = Options()
  chrome_options.add_argument('--headless')
  driver = webdriver.Chrome(executable_path='./chromedriver',chrome_options=chrome_options)
  driver.set_window_size(1200, 741)
  driver.implicitly_wait(2)
  print('初始化中...')
  driver.get("http://x*****e")
  print('填写登录信息中...')
  acc = driver.find_element_by_id('login-email')
  pwd = driver.find_element_by_id('login-pass')
  btn = driver.find_element_by_tag_name('button')
  acc.send_keys('***')
  pwd.send_keys('***')
  btn.click()
  print('跳转到验证码页面中...')
  time.sleep(2)
  capta = driver.find_element_by_id('code')
  capta_input = input('请输入两步验证码:')
  capta.send_keys(capta_input)
  btn1 = driver.find_element_by_tag_name('button')
  btn1.click()
  time.sleep(2)
  print('跳转到创意编辑页面中...')
  return driver

def get_screen(driver,urls):
  count = 1
  for url in urls:
    driver.get(url)
    print('正在抓取--> %s'% url)
    count +=1
    time.sleep(2)
    uid = url.split('/')[-2]
    cid = url.split('/')[-5]
    driver.get_screenshot_as_file("./screen_shot/{}-{}.png".format(uid,cid))
    print("创意--> {}-{}.png 已经保存".format(uid,cid))
    print('还剩 %s 个'% str(len(urls)-count))

def crop_img():
  for img in os.listdir('./screen_shot'):
    if img.endswith('.png'):
      print('%s裁剪中。。'% img)
      im = Image.open('./screen_shot/%s'% img)
      x = 755
      y = 162
      w = 383
      h = 346
      region = im.crop((x, y, x+w, y+h))
      region.save("./screenshot_final/%s" % img)


if __name__ == '__main__':
  driver = login()
  get_screen(driver,all_urls)
  driver.quit()
  print('所有抓取结束')
  crop_img()
  print('所有裁剪结束')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

对python实现二维函数高次拟合的示例详解

在参加“数据挖掘”比赛中遇到了关于函数高次拟合的问题,然后就整理了一下源码,以便后期的学习与改进。 在本次“数据挖掘”比赛中感觉收获最大的还是对于神经网络的认识,在接近一周的时间里,研究...

python利用elaphe制作二维条形码实现代码

手机上的二维码识别程序已经做的很好了,“我查查”用起来很不错的 我搜集了几个二维条码生成网站: http://www.morovia.com/free-online-barcode-ge...

详解python中Numpy的属性与创建矩阵

ndarray.ndim:维度 ndarray.shape:形状 ndarray.size:元素个数 ndarray.dtype:元素数据类型 ndarray.itemsize:字节大小...

Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)

使用Python内置函数:bin()、oct()、int()、hex()可实现进制转换。 先看Python官方文档中对这几个内置函数的描述: bin(x) Convert an inte...

python中文分词,使用结巴分词对python进行分词(实例讲解)

python中文分词,使用结巴分词对python进行分词(实例讲解)

在采集美女站时,需要对关键词进行分词,最终采用的是python的结巴分词方法。 中文分词是中文文本处理的一个基础性工作,结巴分词利用进行中文分词。 其基本实现原理有三点: 1.基于Tri...