当前位置:   article > 正文

论坛自动回复获取积分_论坛自动回复脚本

论坛自动回复脚本
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Mar 21 11:45:12 2022
  4. @author: lenovo
  5. """
  6. #书香门第
  7. # -*- coding: utf-8 -*-
  8. from selenium import webdriver
  9. import time
  10. import re
  11. from selenium.webdriver.common.keys import Keys
  12. class novel(object):
  13. def __init__(self):
  14. # 开始时的url
  15. self.start_url = "http://www.txtnovel.top/forum-95-3.html"
  16. #实例化谷歌设置选项
  17. option = webdriver.ChromeOptions()
  18. #添加保持登录的数据路径:安装目录一般在C:\Users\lenovo\AppData\Local\Google\Chrome\User Data
  19. option.add_argument(r"user-data-dir=C:\Users\lenovo\AppData\Local\Google\Chrome\UserDefault")
  20. # 实例化一个Chrome对象
  21. self.driver = webdriver.Chrome(options=option)
  22. self.novellist =[]
  23. def __del__(self):
  24. self.driver.quit()
  25. #用了option就不用登陆了
  26. def login(self):
  27. try:
  28. #找到登录框,输入账号密码
  29. self.driver.find_element_by_xpath("//*[@id='ls_username']").send_keys("")
  30. self.driver.find_element_by_xpath("//*[@id='ls_password']").send_keys("")
  31. self.driver.find_element_by_xpath("//*[@id='ls_cookietime']").click()
  32. #模拟点击登录
  33. self.driver.find_element_by_xpath("//*[@id='lsform']/div/div/table/tbody/tr[2]/td[3]/button").click()
  34. print("登录成功")
  35. except:
  36. print("登录失败")
  37. def get_content(self):
  38. # 先让程序两秒,保证页面所有内容都可以加载出来
  39. time.sleep(2)
  40. # 获取进入下一页的标签
  41. next_page = 'false'
  42. next_page = self.driver.find_element_by_link_text('下一页')
  43. # 提取需要的数据
  44. try:
  45. elements = self.driver.find_elements_by_xpath("//tbody[starts-with(@id,'normalthread')]/tr/th/a[1]")
  46. for i in elements:
  47. novelurl = i.get_attribute('href')
  48. self.novellist.append(novelurl)
  49. except:
  50. print("爬取"+str(self.driver.current_url)+"小说列表失败")
  51. # 返回下一页标签
  52. return next_page
  53. def reply(self):
  54. self.driver.find_element_by_xpath("//*[@id='fastpostmessage']").send_keys("谢谢楼主分享")
  55. self.driver.find_element_by_xpath("//*[@id='fastpostsubmit']").click()
  56. time.sleep(2)
  57. def run(self):
  58. # 启动chrome并定位到相应页面
  59. self.driver.get(self.start_url)
  60. time.sleep(3)
  61. page = 0
  62. #self.login()
  63. #time.sleep(2)
  64. while True:
  65. page += 1
  66. # 开始提取数据,并获取下一页的元素
  67. next_page = self.get_content()
  68. if page > 2:
  69. break
  70. if next_page == 'false':
  71. break
  72. # 点击下一页
  73. next_page.click()
  74. for url in self.novellist:
  75. self.driver.get(url)
  76. time.sleep(18)
  77. self.driver.find_element_by_xpath("//*[@id='fastpostmessage']").send_keys("谢谢楼主分享")
  78. self.driver.find_element_by_xpath("//*[@id='fastpostsubmit']").click()
  79. def test():
  80. #实例化谷歌设置选项
  81. option = webdriver.ChromeOptions()
  82. #添加保持登录的数据路径:安装目录一般在C:\Users\lenovo\AppData\Local\Google\Chrome\User Data
  83. option.add_argument(r"user-data-dir=C:\Users\lenovo\AppData\Local\Google\Chrome\UserDefault")
  84. # 实例化一个Chrome对象
  85. driver = webdriver.Chrome(options=option)
  86. driver.get("http://www.txtnovel.top/thread-4010493-1-1.html")
  87. time.sleep(2)
  88. driver.find_element_by_xpath("//*[@id='fastpostmessage']").send_keys("谢谢楼主分享")
  89. time.sleep(1)
  90. driver.find_element_by_xpath("//*[@id='fastpostsubmit']").click()
  91. time.sleep(20)
  92. if __name__=='__main__':
  93. novel_spider = novel()
  94. novel_spider.run()
  95. #test()

加入了每小时50回复的限制

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Mar 21 11:45:12 2022
  4. @author: lenovo
  5. """
  6. #书香门第刷金币
  7. #每小时限制发帖50个
  8. # -*- coding: utf-8 -*-
  9. from selenium import webdriver
  10. import time
  11. import re
  12. import os
  13. from selenium.webdriver.common.keys import Keys
  14. class novel(object):
  15. def __init__(self):
  16. # 开始时的url
  17. self.start_url = "http://www.txtnovel.top/forum-95-"
  18. self.start_page =5
  19. #实例化谷歌设置选项
  20. option = webdriver.ChromeOptions()
  21. #添加保持登录的数据路径:安装目录一般在C:\Users\lenovo\AppData\Local\Google\Chrome\User Data
  22. option.add_argument(r"user-data-dir=C:\Users\lenovo\AppData\Local\Google\Chrome\UserDefault")
  23. # 实例化一个Chrome对象
  24. self.driver = webdriver.Chrome(options=option)
  25. self.novellist =[]
  26. def __del__(self):
  27. self.driver.quit()
  28. def login(self):
  29. try:
  30. #找到登录框,输入账号密码
  31. self.driver.find_element_by_xpath("//*[@id='ls_username']").send_keys("")
  32. self.driver.find_element_by_xpath("//*[@id='ls_password']").send_keys("")
  33. self.driver.find_element_by_xpath("//*[@id='ls_cookietime']").click()
  34. #模拟点击登录
  35. self.driver.find_element_by_xpath("//*[@id='lsform']/div/div/table/tbody/tr[2]/td[3]/button").click()
  36. print("登录成功")
  37. except:
  38. print("登录失败")
  39. def get_content(self):
  40. # 先让程序两秒,保证页面所有内容都可以加载出来
  41. time.sleep(2)
  42. # 获取进入下一页的标签
  43. next_page = 'false'
  44. next_page = self.driver.find_element_by_link_text('下一页')
  45. # 提取需要的数据
  46. try:
  47. elements = self.driver.find_elements_by_xpath("//tbody[starts-with(@id,'normalthread')]/tr/th/a[1]")
  48. for i in elements:
  49. novelurl = i.get_attribute('href')
  50. self.novellist.append(novelurl)
  51. except:
  52. print("爬取"+str(self.driver.current_url)+"小说列表失败")
  53. # 返回下一页标签
  54. return next_page
  55. def reply(self):
  56. file = open('novellist.txt', 'r', encoding='utf-8')
  57. self.novellist = eval(file.read())
  58. file.close()
  59. for i in range(50):
  60. url = self.novellist[i]
  61. self.driver.get(url)
  62. time.sleep(15)
  63. self.driver.find_element_by_xpath("//*[@id='fastpostmessage']").send_keys("谢谢楼主分享")
  64. self.driver.find_element_by_xpath("//*[@id='fastpostsubmit']").click()
  65. time.sleep(5)
  66. del self.novellist[0 : 50]
  67. file1 = open('novellist.txt', 'w', encoding='utf8')
  68. file1.write(str(self.novellist))
  69. file1.close()
  70. #每次加载三页的url
  71. def geturl(self):
  72. # 启动chrome并定位到相应页面
  73. self.driver.get(self.start_url+ str(self.start_page) +'.html')
  74. time.sleep(3)
  75. #self.login()
  76. #time.sleep(2)
  77. pagenum = 0
  78. while True:
  79. # 开始提取数据,并获取下一页的元素
  80. next_page = self.get_content()
  81. pagenum += 1
  82. self.start_page += 1
  83. if pagenum >= 3:
  84. file1 = open('novellist.txt', 'w', encoding='utf8')
  85. file1.write(str(self.novellist))
  86. file1.close()
  87. break
  88. if next_page == 'false':
  89. break
  90. # 点击下一页
  91. next_page.click()
  92. def run(self):
  93. while True:
  94. nowlen = len(self.novellist)
  95. if nowlen < 50:
  96. self.geturl()
  97. self.reply()
  98. time.sleep(60*60)
  99. def test():
  100. #实例化谷歌设置选项
  101. option = webdriver.ChromeOptions()
  102. #添加保持登录的数据路径:安装目录一般在C:\Users\lenovo\AppData\Local\Google\Chrome\User Data
  103. option.add_argument(r"user-data-dir=C:\Users\lenovo\AppData\Local\Google\Chrome\UserDefault")
  104. # 实例化一个Chrome对象
  105. driver = webdriver.Chrome(options=option)
  106. driver.get("http://www.txtnovel.top/thread-4010493-1-1.html")
  107. time.sleep(2)
  108. driver.find_element_by_xpath("//*[@id='fastpostmessage']").send_keys("谢谢楼主分享")
  109. time.sleep(2)
  110. driver.find_element_by_xpath("//*[@id='fastpostsubmit']").click()
  111. time.sleep(20)
  112. if __name__=='__main__':
  113. novel_spider = novel()
  114. novel_spider.run()
  115. #test()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/517558
推荐阅读
相关标签
  

闽ICP备14008679号