#描述
脚本一共有两版,分别使用了BeautifulSoup+selenium +chrome和BeautifulSoup+selenium +firefox。实现了对于某网站刷投票的功能。
1.该网站投票选项每次刷新页面后会重新随机排列,但是id不可能改变,因此可以通过 browser.find_element_by_id(ID).click(),进行操作。
2.该网站选满有且只有10个选项才能投票,并且会弹窗提示。
3.该网站限制IP投票次数。
4.该网站限制每次选择选项的间隔时间。
5.网站是ajax异步更新。
#准备工作
####下载chrome浏览器和chromedriver
chromedriver下载地址:
http://chromedriver.storage.googleapis.com/index.html (有墙)
http://npm.taobao.org/mirrors/chromedriver/ (无墙)
####下载火狐浏览器和geckodriver
geckodriver 下载地址:
https://github.com/mozilla/geckodriver/releases/
##BeautifulSoup+selenium +chrome
from bs4 import BeautifulSoup
from selenium import webdriver
import time
from random import sample,choice
import requests
import re
def scanWeb(addr,ips=[]):
"""执行刷票"""
success=0
for num,ipport in enumerate(ips):
result=str(num)+". 代理:"+ipport
#设置headers
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')
options.add_argument('user-agent="'+selectUserAgent()+'"')
#设置代理
options.add_argument('--proxy-server=http://'+ipport)
browser = webdriver.Chrome(chrome_options=options)
browser.set_page_load_timeout(10) #设置超时报错
browser.set_script_timeout(10) #设置脚本超时时间。
try:
browser.get(addr)
except:
print("加载页面太慢,停止加载,继续下一步操作")
browser.execute_script("window.stop()")
#等待页面更新完毕
time.sleep(3)
sltI=selectItemID()
#给10个项目投票
try:
for ID in sltI:
browser.find_element_by_id(ID).click()
time.sleep(1)
# js2 = "var q=document.getElementById('"+ID+"').click()"
# browser.execute_script(js2)
#消除屏幕弹窗
time.sleep(1)
al = browser.switch_to_alert()
al.accept()
browser.find_element_by_class_name("btn")<

 一个刷投票小脚本&spm=1001.2101.3001.5002&articleId=79031252&d=1&t=3&u=b52257760b7c420fbb4a4dc0e8bfd0cf)
1713

被折叠的 条评论
为什么被折叠?



