1、全代码
import requests
from selenium import webdriver
import os
wd = webdriver.Chrome(r'D:\tools-work\chromedriver_win32\chromedriver.exe')
wd.implicitly_wait(5)
# wd.get(f'https://www.szzhdj.gov.cn/views/learn/details.html?type=video&id=e02e7f58f5314d968ef6a9862fa9272b')
wd.get(f'https://www.szzhdj.gov.cn/views/learn/details.html?type=video&id=283c0759306c41c8bf738552a9f83b19')
# #标题
# text = wd.find_elements_by_xpath('//*[@id="app"]/main/section/main/article/p/span')
#标题
title = wd.find_element_by_css_selector('#app > main > section > main > h1')
# print(title.text)
# for x in content:
# print(x.text)
# 视频
MP4 = wd.find_element_by_css_selector('#initVideo > div > video')
# for y in MP4:
URL = MP4.get_attribute('src')
# print(URL)
m = requests.get(URL)
# # for r in range(3):
path = 'D://党建视频//'+ f'{title.text}'+'.mp4'
with open(path, 'wb') as f:
f.write(m.content)
#封面视频
img = wd.find_element_by_css_selector('#initVideo > div > div.vcp-poster > img')
# for y in MP4:
URL1 = img.get_attribute('src')
# print(URL)
m1 = requests.get(URL1)
# # for r in range(3):
path2 = 'D://党建视频封面//'+ f'{title.text}'+'.png'
with open(path2, 'wb') as f:
f.write(m1.content)
quit()
2、分析
2.1 用selenium 取出新闻的标题、视频、封面图片等信息
2.2 用requets库取出视频和图片等信息
参考是关于视频和图片的获取法子
详细可参考:https://blog.csdn.net/weixin_41665637/article/details/114060151?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161951503516780255288933%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=161951503516780255288933&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_v29-1-114060151.nonecase&utm_term=%E8%A7%86%E9%A2%91&spm=1018.2226.3001.4450