#Python+selnium+发邮件+代理IP++自动截图到本地+自动填写表单脚本
自己改一下自己的邮箱和smtp密码就可以用了
# author:dxb time:2020/2/23 0023
#-*- coding:utf8 -*-
# coding=gbk
from selenium import webdriver
import xlrd
import time
from datetime import datetime
from pathlib import Path
import requests
import random
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
driver = webdriver.Chrome()
# 设置浏览器窗口的位置和大小
driver.set_window_position(20,40)
driver.set_window_size(1100,700)
class IP:
def __init__(self,headers):
self.headers=headers
def get_ip_list(self):
#获取ip列表
print("正在获取代理列表...")
url = 'http://www.xicidaili.com/nn/'
html = requests.get(url=url, headers=self.headers).text
soup = BeautifulSoup(html, 'lxml')
#BeautifulSoup最好这样写soup=BeautifulSoup(html,"lxml")
ips = soup.find(id='ip_list').find_all('tr')
ip_list = []
for i in range(1, len(ips)):
ip_info = ips[i]
tds = ip_info.find_all('td')
ip_list.append(tds[1].text + ':' + tds[2].text)
print("代理列表抓取成功.")
return ip_list
#返回ip地址和端口号组成的列表
def get_random_ip(self, ip_list):
#设置随机ip
print("正在设置随机代理...")
proxy_list = []
for ip in ip_list:
proxy_list.append('http://' + ip)
#把ip添加到proxy_list中
proxy_ip = random.choice(proxy_list)
#随机选取出一个ip
proxies = {'http': proxy_ip}
print("代理设置成功.")
return proxies
def start(self):
#开始函数
ip=self.get_ip_list()
print(self.get_random_ip(ip))
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
#我们找到一个写好的headers
ip=IP(headers)
#ip是IP类的一个对象实例
#初始化ip这个实例
def readexcel(file, sheet_index=0):
workbook = xlrd.open_workbook(file)
sheet = workbook.sheet_by_index(sheet_index)
data = []
for i in range(0, sheet.nrows):
data.append(sheet.row_values(i))
for i in range(sheet.nrows):
ip.start()
driver.get("http://47.99.236.126:82/")
driver.find_element_by_id("number").send_keys(str(int(data[i][1])))
driver.find_element_by_id("name").send_keys(data[i][0])
driver.find_element_by_class_name("btn").click()
currUrl = driver.current_url
driver.get(currUrl)
driver.find_element_by_id("temp").send_keys(str(data[i][2]))
driver.find_element_by_id("city").send_keys(data[i][3])
driver.find_element_by_id("county").send_keys(data[i][4])
driver.find_element_by_id("submit").click()
time.sleep(1)
#截图部分代码
scrpath = "C:\\Users\\dell\\Desktop\\selnium_疫情上报" # 指定的保存目录
capturename = '\\' + str(data[i][1]) +"_"+str(data[i][0])+"_"+str(hour)+"_"+str(minute)+"_"+str(second)+'.png' # 自定义命名截图
wholepath = scrpath + capturename
if Path(scrpath).is_dir():
pass
else:
Path(scrpath).mkdir()
while Path(wholepath).exists():
capturename = '\\' +str(data[i][1]) +"_"+str(data[i][0]) +"_"+str(hour)+"—"+str(minute)+ "_"+str(second)+'.png'
wholepath = scrpath + capturename
driver.get_screenshot_as_file(wholepath) # 不能接受Path类的值,只能是字符串,否则无法截图
my_sender = '1204712609@qq.com' # 发件人邮箱账号
my_pass = '*****' # 发件人邮箱密码
my_user = data[i][5]
msg = MIMEText('信息已经发送', 'plain', 'utf-8')
msg['From'] = formataddr(["党小板", my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To'] = formataddr([data[i][0], my_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject'] = "消息发布完毕" # 邮件的主题,也可以说是标题
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.sendmail(my_sender, [my_user, ], msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
return data
hour = datetime.now().hour
minute = datetime.now().minute
second=datetime.now().second
#2:42开始运行函数
#if(hour==2):
# if(minute==42):
# readexcel(r'C:\Users\dell\Desktop\test.xlsx')
readexcel(r'C:\Users\dell\Desktop\selnium_疫情上报\test.xlsx')