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
,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 .
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 intégré danscommande , environ utiliser à vos propres Baidu Google.
$ crontab -e
Si cela est la première fois, il devrait vous permettre de choisir l'éditeur, j'ai choisi , puis ajoutez la ligne dans la dernière ligne
0 0 * * * python ~/dailyReport.py >> ~/dailyReport.log
Cela peut être fait tous les jours exécuter automatiquement le script.
note
Chrome dans la course référence du serveur Bowen: https: //blog.csdn.net/fengmm521/article/details/79661771