bilibil

python 复制代码
import os
import time
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from pywinauto.application import Application
import win32clipboard as w
from selenium.webdriver.common.by import By
import os
import json
import requests
import threading
import re
import string
import random
import time
from urllib.parse import unquote
from concurrent.futures import ThreadPoolExecutor
# from bs4 import BeautifulSoup
# from moviepy.editor import VideoFileClip, AudioFileClip
from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    "cookie": "",
    'Origin': 'https://www.bilibili11.com/',

}


def setText(aString):  # 写入剪切板
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardText(aString)
    w.CloseClipboard()


def run_Chrome():
    app = Application().start(
        r'c:\WINDOWS\System32\cmd.exe /c cd D:\\chrome-win64\\chrome-win64 && start chrome.exe --remote-debugging-port=9999',
        create_new_console=True, wait_for_idle=False)
    time.sleep(3)  # 等待Chrome启动
    # 设置Chrome WebDriver选项
    # 使用selenium打开浏览器
    options = webdriver.ChromeOptions()
    options.add_argument('--disable-extensions')
    options.add_argument('--no-sandbox')  # run Chrome use root
    options.add_argument('--disable-setuid-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--disable-gpu')
    options.add_experimental_option('excludeSwitches', ['enable-automation'])

    from selenium.webdriver.chrome.service import Service

    service = Service(r"D:\chrome-win64\chrome-win64\chromedriver.exe")
    driver = webdriver.Chrome(service=service)

    try:
        # # # 尝试获取顶层窗口
        # window = app.top_window()
        # # 获取所有窗口的句柄
        # window_handles = driver.window_handles

        # 切换到第二个窗口
        # driver.switch_to.window(window_handles[1])
        # driver.implicitly_wait(3)
        driver.get("https://www.bilibili.com/")
        # driver.implicitly_wait(3)
        # window.wait('ready')
        time.sleep(3)  # 等待窗口就绪
        # window.close()
    except Exception as e:
        print("Error occurred while getting top window:", e)

    return driver


def get_user_browser():
    options = Options()
    options.add_experimental_option("debuggerAddress", "127.0.0.1:9999")
    driver = webdriver.Chrome(options=options)
    driver.get('https://www.bilibili.com/')
    time.sleep(15)
    driver.get(url)
    time.sleep(15)
    return driver


def extract_audio(audio_file_path, mp3_path):
    video_clip = None
    audio = None
    try:
        video_clip = VideoFileClip(audio_file_path)
        audio = video_clip.audio
        audio.write_audiofile(mp3_path)
    # except Exception as e:
    #     print(f"An error occurred: {e}")
    finally:
        if video_clip is not None:
            video_clip.close()
        if audio is not None:
            audio.close()


# 示例
def generate_random_code(length=5):
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))


def run(url):
    driver = run_Chrome()
    driver.get(url)
    # result = []
    # try:
    #     for j in range(8):
    #
    #         time.sleep(1)
    #
    #
    #         for i in driver.find_elements(By.CSS_SELECTOR, "[class='bili-video-card__info--right']"):
    #
    #             if 'www.bilibili.com' in i.find_element(By.TAG_NAME, "a").get_attribute('href') and i.find_element(
    #                     By.TAG_NAME, "a").get_attribute('href') not in result:
    #                 hhh=i.find_element(By.TAG_NAME, "a").get_attribute('href')
    #                 result.append(hhh)
    #                 print(hhh)
    #         print(len(result))
    #         driver.find_element(By.XPATH,
    #                         '//*[@id="i_cecream"]/div/div[2]/div[2]/div/div/div[2]/div/div/button[10]').click()
    # except:
    #     pass
    # with open(r"D:\2025\0908\input\915\pipeline\aidcpipeline\novel_data\novel-dialogue-extraction\miwu.txt", 'a+', encoding='utf-8') as f:
    #     for rr in result:
    #         f.write(f'{rr}\n')


    with open(r"D:\2025\0908\input\915\pipeline\aidcpipeline\novel_data\novel-dialogue-extraction\miwu.txt", 'r', encoding='utf-8') as file:
        # 逐行读取文件内容
        for line in file:
            url = line.strip()  # 去除行尾换行符和首尾空格
            if url:
                print(url)
                driver.get(url)
                time.sleep(3.5)
                try:
                    name=""
                    time_d = ""
                    title = ""

                    name = driver.find_element(By.CSS_SELECTOR, "[class='up-detail-top']").text
                    print(name)
                    try:
                        time_d = driver.find_elements(By.CSS_SELECTOR, "[class='pubdate-ip-text']")[1].text
                        print(time_d)
                    except Exception as e:
                        print("ddddd", e)
                    try:
                        title = driver.find_element(By.CSS_SELECTOR, "[class='video-info-title']").text
                        print(title)

                    except Exception as e:
                        try:
                            title = driver.find_element(By.CSS_SELECTOR, "h1[class='video-title special-text-indent']").get_attribute('data-title')
                            print(title)
                        except Exception as e:
                            print("fffff",e)


                    try:

                        data = {
                            'url': url,
                            'name': name,
                            'time_d': time_d,
                            'title': title
                        }
                        with open(r"D:\2025\0908\input\915\pipeline\aidcpipeline\novel_data\novel-dialogue-extraction\miwu.jsonl", 'a',
                                  encoding='utf-8') as jsonl_file:
                            jsonl_file.write(json.dumps(data, ensure_ascii=False) + '\n')
                    except:
                        print("pass+++++")

                except Exception as e:
                    print(e)

    driver.close()



if __name__ == "__main__":

    url = "https://search.bilibili.com/all?keyword=%E5%9C%B0%E9%93%81%E9%80%83%E7%94%9F%E8%BF%B7%E9%9B%BE%E8%8D%92%E5%B2%9B&from_source=webtop_search&spm_id_from=333.1007&search_source=5&duration=2"
    run(url)

https://googlechromelabs.github.io/chrome-for-testing/#stable

相关推荐
第一程序员8 分钟前
Rust trait 入门:把 AI 客户端抽象成可替换接口
python·rust·github
Ulyanov36 分钟前
Python实现6-DOF刚体仿真器(下)——环境扰动与控制闭环
开发语言·python·算法·系统仿真·雷达电子对抗·导引头
小小的木头人1 小时前
Python 批量解析 Excel 经纬度,调用高德地图 API 获取中文地址
开发语言·python·excel
金銀銅鐵1 小时前
[Python] 为 Vole 机器语言实现图形化界面
python·程序员
小林ixn2 小时前
Python基础全梳理:从注释到函数,这些细节你都掌握了吗?
python·编程语言
今天AI了吗2 小时前
Hermes Agent 搭建全流程:从本机试跑到可持续运行的个人 AI Agent
java·人工智能·python·学习·embedding
为啥全要学3 小时前
自注意力中随着根号dk的增大,qk点积的方差为什么也会增大
python·深度学习
weipt4 小时前
用Ollama开发聊天程序实战
python·ollama
来两个炸鸡腿4 小时前
【Datawhale2607】llm-algo-leetcode task02 基础算子
人工智能·python·大模型
庵中十三居士4 小时前
【纯AI无人工修改】AI Agent从0到1实战:50行Python手写核心循环,一次看懂所有Agent框架的底层逻辑
开发语言·人工智能·python