selenium实现后台24小时平均温、全国降水量自动上传工作
注意: 需提前下载好四张图
#!/usr/bin/env python
# coding=utf-8
# 实现pos24小时平均温、全国降水量自动上传工作
# 1.jpg代表 旱涝监测预报
# 2.jpg代表 全国降水量预报
# 3.jpg代表 24小时平均温
# 4.jpg代表 天气预报
import time,os
import requests
from lxml import etree
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from debug_info import Log_info
class PosTqyb():
def __init__(self):
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
self.driver = webdriver.Chrome(options=option)
self.driver.maximize_window()
# self.driver.implicitly_wait(5)
self.wait = ui.WebDriverWait(self.driver, 5)
self.news_source = '中国天气网'
self.current_directory = os.getcwd() + '/'
def login(self,logger):
while True:
self.driver.get('https://pos.XXXX.com/login.html')
while True:
try:
username = int(input("请输入账号:"))
if username != '':
if len(str(username)) != 11:
logger("请正确输入11位手机号")
else:
self.driver.find_element(By.NAME,'j_username').send_keys(username)
break
except:
logger("请正确输入11位手机号码")
while True:
password = input("请输入密码:")
# 排除回车键
if password != "":
self.driver.find_element(By.NAME, "j_password").send_keys(password)
time.sleep(1)
break
else:
logger("密码不能为空,请重试!")
# 点击登录
self.driver.find_element(By.XPATH,'//*[@id="login"]/button').click()
time.sleep(2)
# 没有找到返回-1
if self.driver.page_source.find('登录失败,错误的手机号码或密码!') != -1:
logger("登录失败,请重试,错误的手机号码或密码!")
else:
logger("系统登录中....")
time.sleep(5)
# 点击内容管理
self.driver.find_element(By.LINK_TEXT, '内容管理').click()
logger("点击内容管理")
return True
# 登陆到pos后的操作--内容管理操作
def select_activity(self,logger):
time.sleep(1.5)
logger("点击文章编辑")
self.wait.until(lambda driver: self.driver.find_element(By.LINK_TEXT, '文章编辑'))
self.driver.find_element(By.LINK_TEXT, '文章编辑').click()
time.sleep(2)
self.wait.until(lambda driver: self.driver.find_element(By.LINK_TEXT, '新增文章'))
self.driver.find_element(By.LINK_TEXT, '新增文章').click()
time.sleep(2)
# 选择文章一级类型,这里是下拉菜单
s1 = self.driver.find_element(By.ID, 'articleId1')
Select(s1).select_by_visible_text('农业气象')
time.sleep(1)
def pos24Hour(self,logger):
news_title = '24小时平均温'
image_file_name = '3.jpg'
try:
self.up_news(logger,image_file_name,news_title)
except Exception as e:
logger(e)
with open(news_title+".html","w+",encoding='utf-8')as f:
f.write(self.driver.page_source)
def qgjsyb(self,logger):
news_title = '全国降水量预报'
image_file_name = '2.jpg'
try:
self.up_news(logger,image_file_name,news_title)
except Exception as e:
logger(e)
with open(news_title+".html","w+",encoding='utf-8')as f:
f.write(self.driver.page_source)
def hljcyb(self,logger):
news_title = '旱涝监测预报'
image_file_name = '1.jpg'
# 是否上传封面图,默认True
is_upload_image = False
try:
self.up_news(logger,image_file_name,news_title,is_upload_image)
except Exception as e:
logger(e)
with open(news_title+".html","w+",encoding='utf-8')as f:
f.write(self.driver.page_source)
def wltqyb(self,logger):
# news_title = '天气预报'
image_file_name = '4.jpg'
# 标识是未来天气预报
is_wltq = True
try:
# 要先获取正文内容
(news_title,wltq_content) = self.get_content(logger)
self.up_news(logger,image_file_name,news_title,is_wltq=is_wltq,wltq_content=wltq_content)
except Exception as e:
logger(e)
with open("天气预报.html","w+",encoding='utf-8')as f:
f.write(self.driver.page_source)
# 天气预报获取正文
def get_content(self,logger):
logger("获取未来三天天气标题和正文")
url = 'http://www.weather.com.cn/index/zxqxgg1/new_wlstyb.shtml'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1326.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63010200)'}
res = requests.get(url,headers = headers)
res.encoding = 'utf-8'
html = etree.HTML(res.text)
news_title = html.xpath('/html/body/div[4]/div[1]/div[2]/div/div[1]/span/text()')[0]
content = html.xpath('/html/body/div[4]/div[1]/div[2]/div/div[2]')[0]
result = etree.tostring(content,encoding='utf-8').decode()
wltq_content = result.replace('class="pageContent"', 'class="pageContent" style=font-size:16px')
return (news_title,wltq_content)
# 文章上传处理
def up_news(self,logger,image_file_name,news_title,is_upload_image=True,is_wltq=False,wltq_content=''):
logger("处理%s"%news_title)
image_path = self.current_directory + image_file_name
if os.path.isfile(image_path):
# 点文章编辑及选择第一个分类框
self.select_activity(logger)
time.sleep(1)
# 选择第二个分类的下拉框
s2 = self.driver.find_element(By.ID, 'articleId2')
if is_wltq:
logger("选择天气预报下拉框")
Select(s2).select_by_visible_text('天气预报')
else:
Select(s2).select_by_visible_text(news_title)
time.sleep(1)
# 点击下一步
self.driver.find_element(By.XPATH, '//*[@id="articleForm"]/p[2]/button').click()
time.sleep(2)
logger("发送PAGE_DOWN")
self.driver.find_element(By.XPATH, '//body').send_keys(Keys.PAGE_DOWN)
# 文章标题
self.driver.find_element(By.XPATH, '//*[@id="articleForm"]/p[1]/span/input').send_keys(news_title)
time.sleep(1)
# 传封面图
if is_upload_image:
self.driver.find_element(By.ID,'image').send_keys(image_path)
logger("封面图已添加完毕!")
time.sleep(1)
# 来源
self.driver.find_element(By.XPATH, '//*[@id="source"]').send_keys(self.news_source)
time.sleep(1)
# 关键字和标题一致
self.driver.find_element(By.XPATH, '//*[@id="articleForm"]/p[5]/span/input').send_keys(news_title)
#文章导读和标题一致
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="desc"]').send_keys(news_title)
time.sleep(1)
# 富文本框处的操作:如果是未来三天天气预报则要点击源码
if is_wltq:
logger("上传HTML代码")
# 点击富文本框的第一个HTML代码按钮
self.driver.find_element(By.XPATH,'//*[@id="articleForm"]/p[7]/span/table/tbody/tr[1]/td/table/tbody/tr/td/table[1]/tbody/tr/td[1]/a/span').click()
time.sleep(1)
# 点击文本框准备输入内容
self.driver.find_element(By.CLASS_NAME, 'ke-textarea').send_keys(wltq_content)
else:
logger("上传图片")
# 点击富文本框的图片上传按钮
self.driver.find_element(By.XPATH, '//*[@id="articleForm"]/p[7]/span/table/tbody/tr[1]/td/table/tbody/tr/td/table[2]/tbody/tr/td[13]/a/span').click()
# 切换iframe
frame1 = self.driver.find_element(By.CLASS_NAME,'ke-dialog-iframe')
self.driver.switch_to.frame(frame1)
# 点击本地上传按钮
time.sleep(1)
self.driver.find_element(By.XPATH,'//*[@id="tabNavi"]/ul/li[2]').click()
time.sleep(1)
# 选择图片地址
self.driver.find_element(By.ID,'imgFile').send_keys(image_path)
time.sleep(1)
# 清空图片说明
self.driver.find_element(By.ID,'imgTitle').clear()
time.sleep(1)
self.driver.switch_to.default_content()
# 点击传完图的确定按钮
time.sleep(1)
self.driver.find_element(By.XPATH,'/html/body/div[5]/div[3]/span[1]/input').click()
time.sleep(1.5)
logger("设置区域")
# 点击设置区域
self.driver.find_element(By.XPATH, '//*[@id="review_list_no"]/tr/td[2]/a/span').click()
time.sleep(4)
# 选择iframe
iframe = self.driver.find_element(By.ID, 'dialog_content')
self.wait.until(lambda driver: iframe)
self.driver.switch_to.frame(iframe)
time.sleep(1.5)
logger("点击确定")
self.driver.find_element(By.XPATH, '/html/body/p').click()
self.driver.switch_to.default_content()
time.sleep(1)
logger("提交")
self.wait.until(lambda driver: self.driver.find_element(By.XPATH, '//*[@id="ckeckAttr33"]'))
self.driver.find_element(By.XPATH, '//*[@id="ckeckAttr33"]').click()
# 添加成功后会有一个添加成功的弹窗
time.sleep(1.5)
self.driver.find_element(By.CLASS_NAME, 'close').click()
time.sleep(2)
self.submit(news_title,image_path,is_wltq=is_wltq)
else:
logger("%s无法上传,未找到图片%s"%(news_title,image_file_name))
# 待提交、待发布处理
def submit(self,news_title,image_path,is_wltq=False):
logger("待提交界面!")
new_file_name = '已上传' + news_title
try:
# 获取自己提交的,获取文章类型字段
add_name = self.driver.find_elements(By.XPATH, '//*[@id="article"]/tbody/tr/td[3]')
length = len(add_name)
if is_wltq:
news_title = '天气预报'
for k in range(length):
if news_title in add_name[k].text:
logger("点击待提交界面的提交!")
self.driver.find_element(By.XPATH, '//*[@id="article"]/tbody/tr[' + str(k + 1) + ']/td[8]/a[3]').click()
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="companyForm"]/p/button').click()
time.sleep(1)
# 添加成功后会有一个添加成功的弹窗
self.driver.find_element(By.CLASS_NAME, 'close').click()
time.sleep(2)
logger("待发布界面!")
# 获取自己提交的
pub_name = self.driver.find_elements(By.XPATH, '//*[@id="article"]/tbody/tr/td[3]')
for j in range(len(pub_name)):
if news_title in pub_name[j].text:
logger("点击待发布提交!")
self.driver.find_element(By.XPATH, '//*[@id="article"]/tbody/tr[' + str(j + 1) + ']/td[8]/a[1]').click()
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="tabs"]/p/button').click()
time.sleep(1)
# 添加成功后会有一个添加成功的弹窗
self.driver.find_element(By.CLASS_NAME, 'close').click()
time.sleep(1.5)
# 对上传的图片进行重命名,证明已上传
dst = self.current_directory + new_file_name + '.jpg'
os.rename(image_path,dst)
# 也可以删除掉图片
# os.remove(image_path)
except Exception as e:
logger("%s出现异常" %news_title)
logger(e)
logger("发送PAGE_UP")
self.driver.find_element(By.XPATH, '//body').send_keys(Keys.PAGE_UP)
if __name__ == '__main__':
logger = Log_info().main()
tqyb = PosTqyb()
# 登录
switch = tqyb.login(logger)
if switch:
# 传24小时平均温度
tqyb.pos24Hour(logger)
# 传全国降水量预报
tqyb.qgjsyb(logger)
# 传旱涝监测预报
tqyb.hljcyb(logger)
# 传未来三天全国天气预报
tqyb.wltqyb(logger)
logger("数据处理完毕!")