相关文章推荐

利用selenium获取bilibili蔡徐坤的搜索内容,并保存到csv文件,请给出可以在spyder上运行的oython代码

4浏览 · 2024-06-10 20:03:47
好的,这里是可以在Spyder上运行的Python[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083): ```python # 导入所需的库 from selenium import webdriver import time import csv # 创建浏览器驱动[对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083) driver = webdriver.Chrome() # 访问Bilibili网站 driver.get("https://www.bilibili.com/") # 等待页面加载完成 time.sleep(3) # 定位搜索框并输入关键词(蔡徐坤) search_box = driver.find_element_by_xpath('//input[@id="nav_searchform-input"]') search_box.send_keys("蔡徐坤") # 点击搜索按钮 search_button = driver.find_element_by_xpath('//button[@class="nav-search-btn"]') search_button.click() # 等待搜索结果加载完成 time.sleep(3) # 定位搜索结果[列表](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083) search_result = driver.find_element_by_xpath('//ul[@id="video-list"]') # 获取搜索结果[列表](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)中每个[视频](https://geek.csdn.net/educolumn/237e06506d30a4cfa9904992fb4f696f?spm=1055.2569.3001.10083)的标题和链接,并保存到CSV文件 with open('bilibili_cxk.csv', 'w', encoding='utf-8', newline='') as f: writer = csv
 
推荐文章