首先先介绍一下Python自动化利器-----selenium,Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome、Firefox、Safari等主流界面浏览器,同时也支持phantomJS无界面浏览器
注入文章主要分为两步,1.登录,2.完成注入
下面完成代码
import requests
from selenium import webdriver
import time
browser = webdriver.Firefox(executable_path='/Users/lyf/Library/Google/geckodriver')
def do_login():
browser.get("https://www.link-nemo.com/login")
email = browser.find_element_by_id('email')
password = browser.find_element_by_id('password')
login = browser.find_element_by_class_name('btn-login')
email.send_keys('账号')
password.send_keys('密码')
login.click()
def addArticle():
browser.get('https://www.link-nemo.com/post')
input_title = browser.find_element_by_id('title')
input_text = browser.find_element_by_class_name('w-e-text')
input_title.send_keys("Python自动注入--测试标题")
input_text.send_keys('Python自动注入--测试内容')
time.sleep(3)
post_article = browser.find_element_by_id('btn-send')
try:
post_article.click()
time.sleep(2)
except Exception as e:
print(e)
if __name__ == "__main__":
do_login()
for i in range(1000):
addArticle()