1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- from selenium.webdriver.common.action_chains import ActionChains
- from selenium.common.exceptions import NoSuchElementException, TimeoutException
- from selenium import webdriver
- import random
- import time
- def reply(driver, url):
- while True:
- reply_page_url = get_reply_page_url(driver, url)
- try:
- driver.get(reply_page_url)
- except TimeoutException:
- continue
- else:
- content = get_reply_content(driver)
- try:
- temp_driver = webdriver.Chrome()
- temp_driver.get('http://fanyi.youdao.com/')
- input_content = temp_driver.find_element_by_id('inputOriginal')
- input_content.send_keys(content)
- time.sleep(3)
- content = temp_driver.find_element_by_xpath("//div[@id='transTarget']/p/span").text
- input_delete = temp_driver.find_element_by_id('inputDelete')
- ActionChains(temp_driver).move_to_element(input_delete).click().perform()
- input_content.send_keys(content)
- time.sleep(3)
- content = temp_driver.find_element_by_xpath("//div[@id='transTarget']/p/span").text
- temp_driver.quit()
- except Exception as e:
- print(e)
- if not content == '#':
- if start_reply(driver, content):
- break
- time.sleep(2)
- # 进行回复动作
- def start_reply(driver, content):
- try:
- message = driver.find_element_by_id('vmessage')
- except NoSuchElementException:
- return False
- else:
- ActionChains(driver).move_to_element(message).perform()
- time.sleep(6)
- ActionChains(driver).double_click(message).perform()
- time.sleep(3)
- message.send_keys(content)
- print(content)
- time.sleep(1)
- element = driver.find_element_by_id('vreplysubmit')
- ActionChains(driver).move_to_element(element).click().perform()
- return True
- # 读取从上往下第二条评论的内容,并返回
- def get_reply_content(driver):
- i = 4
- while i >= 0:
- r = random.randint(3, 12)
- r = str(r)
- try:
- element = driver.find_element_by_xpath(
- "//div[@id='postlist']/div[" + r + "]/table/tbody/tr/td[2]/div[2]/div/div/table/tbody/tr/td")
- except NoSuchElementException:
- i = i - 1
- else:
- content = element.text
- if '发表于' not in content and '本贴最后由' not in content:
- return content
- else:
- i = i - 1
- return '#'
- # 取得mod发布页面的最后一个页面的url
- def get_reply_page_url(driver, url):
- driver.get(url)
- r = random.randint(0, 24)
- r = str(r)
- element = driver.find_element_by_xpath("//form[@id='moderate']/table/tbody[last()-" + r + "]/tr/th/a[3]")
- return element.get_attribute('href')
|