python刷B站播放量

2022-07-31

首先,我们要知道B站播放量计算方法:不同的IP只能算一次,如果点一下就关掉,会出现播放完成度很低的情况,反而不利。
所以我们要做的是:不停换IP地址打开视频播放页,最好能看完视频。
下面是实现方法:
获取某一个网页上的所有代理IP,返回一个IP列表:

def get_ip_list(url, headers):  
    web_data = requests.get(url, headers=headers)  
    soup = BeautifulSoup(web_data.text, 'lxml')  
    ips = soup.find_all('tr')  
    ip_list = []  
    for i in range(1, len(ips)):  
        ip_info = ips[i]  
        tds = ip_info.find_all('td') #tr标签中获取td标签数据  
        ip = tds[1].text + ':' + tds[2].text
        ip_list.append(ip)  
    return ip_list  

代码来自:https://blog.csdn.net/qq_38525781/article/details/81564036
我用xpath和requests的试了一下,不知道为什么不行,但是上面这个是可以的。

从网站上爬取数据(主要是不断的切换页面)

def Get_Ip_Form_66():
    global ip_list
    ip_list=[]
    global headers
    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Cache-Control':'max-age=0',
        'Connection':'keep-alive',
        'Host':'www.66ip.cn',
        'If-None-Match':'W/"b077743016dc54409ebe6b86ba7a869b"',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
    }
    cookies = None
    
    for i in range(1,20):
        url = 'http://www.66ip.cn/'+str(i)+'.html'
        ip_list.append(get_ip_list(url,headers))

然后是自动打开网页。获取视频时间,然后二倍速播放。代理部分参考的是https://www.jb51.net/article/151634.htm

def Auto_Like_Your_Video(url):
    try:
        # 使用代理ip
        chromeOptions = webdriver.ChromeOptions()
        chromeOptions.add_argument("--proxy-server="+str(url))# 一定要注意,=两边不能有空格,不能是这样--proxy-server = http://202.20.16.82:10152
        driver = webdriver.Chrome(options=chromeOptions)
        
        # 打开视频播放页
        driver.get("https://www.bilibili.com/video/BV1hi4y1x7PM")
        time.sleep(7)
        
        # 获取视频时长
        Video_Time = driver.find_element_by_xpath("//div[@name='time_textarea']/span[3]").text
        Total_Second = Change_The_Time_Type(Video_Time)
        
        # 两倍速
        element=driver.find_element_by_xpath("//button[@class='bilibili-player-video-btn-speed-name']")
        webdriver.ActionChains(driver).move_to_element(element).click(element).perform()
        element=driver.find_element_by_xpath("//ul[@class='bilibili-player-video-btn-speed-menu']/li[1]")
        webdriver.ActionChains(driver).move_to_element(element).click(element).perform()
        
        # 点击播放
        element=driver.find_element_by_xpath("//button[@class='bilibili-player-iconfont bilibili-player-iconfont-start']")
        webdriver.ActionChains(driver).move_to_element(element).click(element).perform()
         
        # 页面最小化
        driver.minimize_window() 
        
        # 看完视频
        time.sleep(Total_Second/2)
        
        # 关闭页面
        driver.close()
    except :
        pass

注意的是两倍速必须鼠标移上去才可以点击,所以新增加一个移上去的动作,别的用xpath定位就可以了,没什么麻烦的地方。

主要的不足:必须看完一个视频才能看下一个,而且没有对代理IP池进行检验。后来我试了一下检验,感觉不太行(比如打开百度,找里面的某个元素,看看能不能找到),用多线程的话,不知道为什么,是5个5个打开的,电脑直接就卡死了,页面能打开,但是很卡,无法点击,也不行。
如果大家有好的方法希望能跟我说一下。
完整的代码可以访问:https://github.com/HGGshiwo/AutoLikeYourVideo.git

本文地址:https://blog.csdn.net/HGGshiwo/article/details/107661135

《python刷B站播放量.doc》

下载本文的Word格式文档,以方便收藏与打印。