通过python+selenium3实现浏览器刷简书文章阅读量

yipeiwu_com6年前Python基础

准备工作

下载python,本文以python3.6为例。python3.6下载地址:python3下载地址,选择合适的版本安装。安装成功后,打开命令提示符,在其中输入python,显示如下信息,则说明安装成功。

C:\Users\Ubuntu>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

安装selenium3,打开命令提示符,输入pip install selenium,selenium会自动下载安装。安装完成后,打开命令提示符,输入python,然后在python环境下输入import selenium,如果没有提示错误,则安装成功。

C:\Users\Ubuntu>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
>>>

下载浏览器驱动,本文以火狐为例,火狐的selenium驱动为geckodriver。geckodriver下载地址:geckodriver下载地址,下载解压后,将geckodriver.exe文件放到python目录下,本例地址为C:\Program Files\Python36,也可以将geckodriver.exe加入环境变量。

刷阅读量脚本

实测在未登录情况下,通过刷新页面,可以达到增加阅读量的效果,所以我们的策略是,打开浏览器,不停的刷新页面,以达到增加阅读量的效果。下面是刷数量代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class new_sub_count(unittest.TestCase):
 def setUp(self):
  self.driver = webdriver.Firefox()
  self.driver.implicitly_wait(30)
  self.base_url = 'https://www.jianshu.com/p/93a2895000d3' # 链接为需要刷数量的文章链接
  self.verificationErrors = []
  self.accept_next_alert = True
 
 """刷新阅读量"""
 def test_refresh_count(self):
  driver = self.driver
  driver.get(self.base_url)
  for i in range(100): # 其中数字为要刷新的数量
   time.sleep(2) # 为防止浏览器频繁刷新僵死,故设置休息时间
   driver.refresh() # 刷新
  driver.quit() # 退出浏览器

if __name__ == '__main__':
 unittest.main()

将上文的代码保存为.py格式的文件,文件编码为utf-8格式,本文将文件另存为count.py,并存储在C:\Users\Ubuntu目录下。

执行

打开命令提示符,进入存放文件的目录,执行python count.py,如下:

C:\Users\Ubuntu>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python count.py

执行开始后,程序会调用火狐浏览器,并自动进行刷新,以达到刷阅读数量的效果。本文仅作技术交流,请正当使用~希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Pyhton中防止SQL注入的方法

复制代码 代码如下: c=db.cursor() max_price=5 c.execute("""SELECT spam, eggs, sausage FROM breakfast &...

libreoffice python 操作word及excel文档的方法

1、开始、关闭libreoffice服务; 开始之前同步字体文件时间,是因为创建soffice服务时,服务会检查所需加载的文件的时间,如果其认为时间不符,则其可能会重新加载,耗时较长,因...

详解Python二维数组与三维数组切片的方法

如果对象是二维数组,则切片应当是x[:]的形式,里面有一个冒号,冒号之前和之后分别表示对象的第0个维度和第1个维度; 如果对象是三维数组,则切片应当是x[::],里面有两个冒号,分割出...

对python中Matplotlib的坐标轴的坐标区间的设定实例讲解

对python中Matplotlib的坐标轴的坐标区间的设定实例讲解

如下所示: <span style="font-family: Arial, Helvetica, sans-serif;">>>> import nu...

vscode 远程调试python的方法

vscode 远程调试python的方法

本文介绍了vscode 远程调试python的方法,分享给大家,具有如下: 实验环境 远程服务器:京东云,1核2G,centos7.3 64bit 本地环境配置 安装vscode...