python selenium爬取街舞网站视频数据

2023-10-13 12:30

本文主要是介绍python selenium爬取街舞网站视频数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言


想要做一个街舞网站视频的app,利用爬取的街舞视频存入本地,展现到app上

爬取网站:https://www.vhiphop.com/

Vhiphop网站也是我很喜欢的街舞网站之一,这里聚集了很多我喜欢的舞者视频和教学

爬取代码:


# !/usr/bin/env python
# -*- coding: utf-8 -*-# 爬取Vhiphop网站import requests
import importlib
import lxml.html
import os
import sys
importlib.reload(sys)
import jsonfrom selenium import webdrivervideo_list = []def selenium_get_vhiphop_html(http_url):# 更改代理ipoptions = webdriver.ChromeOptions()options.add_argument("--proxy-server=http://110.73.2.248:8123")#获取chromedriver的绝对路径driver_path = r'E:\fiddler\chromedriver.exe'# 初始化一个driver,并且指定chromedriver的路径driver = webdriver.Chrome(executable_path=driver_path)# driver = webdriver.Chrome(executable_path=driver_path,, chrome_options=options)# 请求网页driver.get(http_url)# 通过page_source获取网页源代码 热键 ctrl+alt+Lreturn driver.page_source# request请求,但response内容隐藏
def parse_vhiphop_url(http_url):#拿到请求头 模拟用户搜索headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}response = requests.get(http_url,headers=headers,verify=False)# 响应首页内容return response.text# 处理html数据,得到所有的数据
def get_vhiphop_video(html_data):"""分析全网数据,并提取视频信息"""# 获得etree对象metree = lxml.html.etree# 获得解析对象parser = metree.HTML(html_data)# 解析并获得div列表li_list = parser.xpath("//ul[@class='video-lists-box']/li")# 遍历div列表for li_element in li_list:# 拿到一个个li标签video_title = li_element.xpath("./a[@class='video-lists-link']/@title")# 40个popping信息item = {}# 获取标题item["video_title"] = video_title[0]# 获取ul地址,以用查找视频地址video_data_html_url = li_element.xpath("./a[@class='video-lists-link']/@href")[0]if not video_data_html_url.startswith("http"):video_data_html_url = "https://www.vhiphop.com"+video_data_html_url# 获取街舞视频播放页面html数据# video_data = selenium_get_vhiphop_html(video_data_html_url)# 解析html数据video_url = video_real_url(video_data_html_url)item["video_url"] = video_url# video封面图片地址video_img_url = li_element.xpath("./a[@class='video-lists-link']/div/img/@data-src")[0]item["video_img_url"] = video_img_url# 获取街舞类型:popping lacking breakingvideo_type = li_element.xpath("./a[@class='video-lists-link']/div[@class='video-meta-box']/div[@class='video-title-box']/span/text()")[0]item["video_type"] = video_typevideo_list.append(item)def selenium_get_video_html(html_url):# 获取chromedriver的绝对路径driver_path = r'E:\fiddler\chromedriver.exe'# 初始化一个driver,并且指定chromedriver的路径driver = webdriver.Chrome(executable_path=driver_path)# driver = webdriver.Chrome(executable_path=driver_path,, chrome_options=options)# 请求网页driver.get(html_url)# 通过page_source获取网页源代码 热键 ctrl+alt+Lreturn driver.page_sourcedef video_real_url(video_html_url):html_data = selenium_get_video_html(video_html_url)# 获得etree对象metree = lxml.html.etree# 获得解析对象parser = metree.HTML(html_data)# 解析并获得divvideo_url = parser.xpath("//div[@class='video-container cp']/div/video[@class='vjs-tech']/@src")return video_urldef result(http_url):item = {}html_data = selenium_get_vhiphop_html(http_url)get_vhiphop_video(html_data)def save_video_datas_json_file(video_list):"""以json格式保存到本地"""# 当目录不存在时则创建新目录video_dir_name = "./videos"if not os.path.exists(video_dir_name):# 创建目录os.makedirs(video_dir_name)# 写入数据 python类型的数据转换为字符串video_json_strs = json.dumps(video_list,ensure_ascii=False,indent=2)# 写入文件video_file = open(video_dir_name+"/video.json","w",encoding="utf-8")video_file.write(video_json_strs)video_file.close()print("总共个数:"+len(video_list))print("视频数据已经保存成功!谢谢!")def main():# 推荐页http_url_index = "https://www.vhiphop.com/"# choreograph = 1# 编舞http_url_choreograph = "https://www.vhiphop.com/videos?sort=1"result(http_url_choreograph)# poppinghttp_url_popping = "https://www.vhiphop.com/videos?sort=2"result(http_url_popping)# hiphophttp_url_hiphop = "https://www.vhiphop.com/videos?sort=3"result(http_url_hiphop)# jazzhttp_url_jazz = "https://www.vhiphop.com/videos?sort=4"result(http_url_jazz)# lockinghttp_url_locking = "https://www.vhiphop.com/videos?sort=5"result(http_url_locking)# wacckinghttp_url_waccking = "https://www.vhiphop.com/videos?sort=6"result(http_url_waccking)# breakinghttp_url_breaking = "https://www.vhiphop.com/videos?sort=7"result(http_url_breaking)# househttp_url_house = "https://www.vhiphop.com/videos?sort=8"result(http_url_house)# contemporaryhttp_url_contemporary = "https://www.vhiphop.com/videos?sort=9"result(http_url_contemporary)# reggaehttp_url_reggae = "https://www.vhiphop.com/videos?sort=10"result(http_url_reggae)# freestlyhttp_url_freestly = "https://www.vhiphop.com/videos?sort=11"result(http_url_freestly)# krumpinghttp_url_krumping = "https://www.vhiphop.com/videos?sort=12"result(http_url_krumping)# othershttp_url_others = "https://www.vhiphop.com/videos?sort=13"result(http_url_others)# 解析网址,请求数据# [{视频1},{视频2},{视频3}]# 保存文件save_video_datas_json_file(video_list)# item_popping = get_vhiphop_video(html_data_index)if __name__ == '__main__':main()

爬取的数据以json存入文本

response内容隐藏

request请求相应的response内容不全

检查网页元素可以看到 response内容被隐藏,可以通过selenium直接获取浏览器的Element

缺点

因为需要安装浏览器驱动,所以每次请求网页时都需要打开浏览器,加载时间实在太长。建议采用多线程爬取,等待时间缩短一些

后续优化

等我写好app了在搞

这篇关于python selenium爬取街舞网站视频数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/203225

相关文章

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.