情景:老板规定在指定日期必须分享文章
使用selenium进行微信的自动化操作,适用requests获取目标网站的内容,适用BeautifulSoup快速获得需要选择的元素及内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
from selenium import webdriver import time import requests from BeautifulSoup import BeautifulSoup browser = webdriver.Chrome() browser.get( "https://wx.qq.com/ ") print( "请登录 ") time.sleep(10) chat_elems = browser.find_elements_by_class_name( "ng-scope ") for elem in chat_elems: if(elem.text.encode('UTF-8') == u'文件传输助手'.encode('UTF-8')): elem.click() break while True: current_time = time.localtime(time.time()) if ((current_time.tm_mday%10 == 5) and (current_time.tm_hour == 1)): headers = { "Accept ": "text/html,application/xhtml+xml,application/xml; ", "Accept-Encoding ": "gzip ", "Accept-Language ": "zh-CN,zh;q=0.8 ", "Referer ": "http://daily.zhihu.com/ ", "User-Agent ": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 " } zhihu = BeautifulSoup(requests.get(url= "http://daily.zhihu.com/ ",headers=headers).text) frist_q = zhihu.find(attrs={'class':'wrap'}).find( "a ") frist_q_content = frist_q.text frist_q_link = 'http://daily.zhihu.com' + frist_q.get( "href ") browser.find_element_by_id( "editArea ").send_keys(frist_q_content) browser.find_element_by_id( "editArea ").send_keys(frist_q_link) browser.find_element_by_class_name( "btn_send ").click() time.sleep(60*59)
|
其中,headers 为了反防爬虫策略。