coup de poing automatique Python +

avant-propos

Il est rappelé aux conseillers encore et encore ces derniers jours a forcé un coup de poing quotidien, je pense à écrire un script pour accrocher sur le serveur chronométré exécution. après monDes efforts inlassables, choisirent s e l e n je en m sélénium ,raison du simple (

bibliothèque de sélénium d'installation

$ sudo pip install selenium

Installation chromdriver

Parce que j'ai une procuration si directement sur le site officiel de téléchargement, puis ici , vous pouvez choisir d'utiliser la source miroir Taobao .
Insérer ici l'image Description
Pour plus de commodité, je mets des commandes directes. Chromedriver je choisis est ici la version 80.0.3987.16 (note télécharger Chrome pendant un certain temps et la même version).

  • télécharger
$ wget https://npm.taobao.org/mirrors/chromedriver/80.0.3987.16/chromedriver_linux64.zip
  • décompression
$ unzip chromedriver_linux64.zip -d . 
  • Dans le répertoire approprié et l'accès exécutable
$ sudo cp chromedriver /usr/bin && sudo chmod +x /usr/bin/chromedriver

installer Chrome

  • Installation dépend
$ sudo apt-get install libxss1 libappindicator1 libindicator7
  • installer Chrome
$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
$ sudo dpkg -i google-chrome*.deb
$ sudo apt-get install -f
  • Version Voir
$ google-chrome --version
  • débogage de test
$ google-chrome --headless --remote-debugging-port=9222 https://chromium.org --disable-gpu

Scripting

  • Créer une autorisations de script et subventions
$ touch dailyReport.py && touch dailyReport.log && sudo chmod +x dailyReport.py
  • teneur
# encoding=utf8
from selenium import webdriver
import time

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox') # 这个配置很重要
client = webdriver.Chrome(chrome_options=chrome_options, executable_path='/home/ubuntu/chromedriver')    # 如果没有把chromedriver加入到PATH(如usr/bin)中,就需要指明路径

try:
    client.get('https://xxxxx/xxx/login')
    name = client.find_element_by_name("username")
    password = client.find_element_by_name('password')
    name.send_keys('用户名')
    password.send_keys('密码')
    login_button = client.find_element_by_xpath('//*[@id="form1"]/div[4]/button')
    login_button.click()
except:
    print(self.getCurrentTime(), u'登录异常!'.encode('utf8'))
else:
    print(u'登录成功!'.encode('utf8'))

try:
    client.get('https://xxxxxx')
    client.get('https://xxxxxx')
    submit_button = client.find_element_by_xpath('//*[@id="p1_ctl00_btnSubmit"]/span/span')
    submit_button.click()
    ensure_button = client.find_element_by_xpath('//*[@id="fineui_26"]/span/span')
    ensure_button.click()
except:
    print(self.getCurrentTime(), u'提交表单异常! 打卡失败!'.encode('utf8'))
else:
    print(u'打卡成功!'.encode('utf8'))
finally:    
    client.quit()
    print(u'浏览器退出...'.encode('utf8'))

# 输出完成时间
print('my python scirpt complete -> ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '\n')

Contenu de la nécessité de script pour effectuer la modification correspondante Selon le site.

exécution du script de synchronisation

Ici, nous utilisons L je n en X Linux intégré danscommande c r la n t une b crontab , environ c r la n t une b crontab utiliser à vos propres Baidu la r ou Google.

$ crontab -e

Si cela est la première fois, il devrait vous permettre de choisir l'éditeur, j'ai choisi v je m vigueur , puis ajoutez la ligne dans la dernière ligne

0 0 * * * python ~/dailyReport.py >> ~/dailyReport.log

Cela peut être fait tous les jours 00 : 00 00:00 exécuter automatiquement le script.

note

Chrome dans la course référence du serveur Bowen: https: //blog.csdn.net/fengmm521/article/details/79661771

Publié 219 articles originaux · a gagné les éloges 23 · vues 30000 +

Je suppose que tu aimes

Origine blog.csdn.net/Eternally831143/article/details/105174883
conseillé
Classement