Through python+selenium3 to achieve the browser reading volume of simple books and articles

  • 2020-06-19 10:57:42
  • OfStack

The preparatory work

Download python and use python3.6 as an example. python3.6 Download address: python3 download address, select the appropriate version to install. After successful installation, open the command prompt, enter python, and display the following information to indicate successful installation.


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.
>>>

To install selenium3, open a command prompt and type pip install selenium. selenium will automatically download the installation. When the installation is complete, open the command prompt, type python, and then type import selenium in the python environment. If no errors are prompted, the installation is successful.


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
>>>

Download the browser driver. Take Firefox as an example. selenium for Firefox is geckodriver. geckodriver download address: geckodriver, download and unzip the geckodriver.exe file into the python directory. In this case, the address is C:\Program Files\Python36, or add geckodriver.exe to the environment variable.

Brush up on the reading script

It is actually measured that the amount of reading can be increased by refreshing the page without logging in. Therefore, our strategy is to open the browser and keep refreshing the page to increase the amount of reading. The following is the brush number code:


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' #  Links are the number of links that need to be brushed 
  self.verificationErrors = []
  self.accept_next_alert = True
 
 """ Refresh reading volume """
 def test_refresh_count(self):
  driver = self.driver
  driver.get(self.base_url)
  for i in range(100): #  Where the number is the number to refresh 
   time.sleep(2) #  Break times are set to prevent browser refresh from hardening 
   driver.refresh() #  The refresh 
  driver.quit() #  Exit browser 

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

Save the above code as a file in.py format, encoded in ES44en-8. Save the file as ES45en.py and store it in the directory C:\Users\Ubuntu.

perform

Open the command prompt, enter the directory where the files are stored, and execute python count.py, as follows:


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

Related articles: