Use python to automatically watch videos from station B

Preface

The main logic of the script is to use timers and multi-threads to implement loop execution of automatic video viewing. The loop_monitor() function calls the views() function by creating a timer object, and executes the views() function every 5 seconds.
The #views() function determines whether the time since the last execution exceeds 1000 seconds. If it exceeds,
a timer object is created to call the view_int() function, and then sleeps for 10 minutes. The view_int() function creates 5 threads, and each thread calls the Auto_Like_Your_Video() function to perform the automatic brushing operation.
In the Auto_Like_Your_Video() function, configure the browser options by setting ChromeOptions, including setting User-Agent, using a proxy, etc.
Then create a webdriver.Chrome object, open the specified web page by calling the get() method, perform a series of operations, and finally exit the browser. If it is determined that the stop() function returns True, the loop will be exited and the playback volume operation will stop.

1. Preparation

proxypool is a free and open source proxy pool. I won’t go into details about the installation. I worked on this all afternoon and it was so bad.

2. Usage steps

1. Import the library

The code is as follows (example):

import random
from selenium import webdriver
from threading import Timer
import time
import requests
import threading
import asyncio
from bilibili_api import video

2.Core code

The code is as follows (example):

def view_int():
    print('初始化成功')
    global state
    do1 = threading.Thread(target=Auto_Like_Your_Video, args=(get_proxy().get("proxy"), state,))
    do2 = threading.Thread(target=Auto_Like_Your_Video, args=(get_proxy().get("proxy"), state,))
    do3 = threading.Thread(target=Auto_Like_Your_Video, args=(get_proxy().get("proxy"), state,))
    do4 = threading.Thread(target=Auto_Like_Your_Video, args=(get_proxy().get("proxy"), state,))
    do5 = threading.Thread(target=Auto_Like_Your_Video, args=(get_proxy().get("proxy"), state,))
    print('线程打开')
    do1.start()
    do2.start()
    do3.start()
    do4.start()
    do5.start()
    time.sleep(2)
    state = True
    print('线程关闭')
    do1.join()
    do2.join()
    do3.join()
    do4.join()
    do5.join()
    state = False

The data requested by the url network used here.


def Auto_Like_Your_Video(url,stop):
    print(stop)
    global b_time
    try:
        USER_AGENT = [
            'Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.229 Version/11.62',
            'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.9.168 Version/11.52',
            'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0',
            'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) Gecko/20130331 Firefox/21.0',
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0',
        ]
        ChromeOptions = webdriver.ChromeOptions()
        ua = random.choice(USER_AGENT)
        ChromeOptions.add_argument('--user-agent=%s' % ua)
        ChromeOptions.add_argument("--proxy-server=" + "http://" + str(url))
        # desired_capabilities = DesiredCapabilities.CHROME
        # desired_capabilities["pageLoadStrategy"] = "none"
        driver = webdriver.Chrome(
            r'xxxxx\chromedriver.exe',
            options=ChromeOptions)
        driver.get('https://www.bilibili.com/video/xxxx')
        if driver.title == 'www.bilibili.com':
            print('页面访问失败')
            driver.close()
        time.sleep(b_time / random.randint(1, 4))
        driver.close()
        if stop:
            print(stop)

    except:
        pass

Summarize

The unresolved problem cannot be deployed on the Linux server. After investigation, it seems that the webdriver has not been successfully enabled. I hope someone can help.

Guess you like

Origin blog.csdn.net/qq_33655643/article/details/131094703