2

十年老Python程序员:给我一个链接,没有我不能爬的视频,只有我顶不住的视频

 2 years ago
source link: https://blog.csdn.net/fei347795790/article/details/121477677
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

一、写在前面

真的,为什么别人发游戏这么多人看,我发了两次了加起来才一百个。

算了算了,不整游戏了,反正你们也不爱看~
在这里插入图片描述
今天来试试把头条上扭腰上热门的那些妹子爬一爬,不知道我顶不顶得住~
在这里插入图片描述

二、准备工作

1、使用的环境
  • python 3.8
  • pycharm 2021.2 专业版
2、要用的第三方模块
  • selenium
  • requests
  • parsel

三、大致流程

鉴于你们不喜欢我啰嗦,但是流程呢,我还是要给你们写出来,所以我就单独把它列出来了。
在这里插入图片描述

1、网站分析(明确需求)
  1. 在视频网页源代码当中找到 embedUrl 对应的链接;
  2. 在链接当中找到视频播放地址,在元素面板当中;
  3. 发现规律 embedUrl上面的 groupby_id 其实就是当前视频链接上的id,下载视频的时候 就只需要 一个 id 就可以下载视频;(https://www.ixigua.com/embed?group_id=7029910152576926238)
2、代码实现过程
  1. 构建embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238
  2. 使用selenium访问该链接
  3. 提取视频链接地址
  4. 拼接视频链接地址
  5. 使用requests发送请求 并且获取视频二进制数据

如果大家在学习Python的过程中不知道学习方向,该怎么学,没有好的系统的学习资料、没人交流解答等等,都可以私我,我都给大家准备好了。
在这里插入图片描述

四、代码展示分析

首先导入一下模块

import requests
from selenium import webdriver

进入浏览器设置

options = webdriver.ChromeOptions()

1、构建embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238

group_id = input("请输入你要下载视频的id:")
url = 'https://www.ixigua.com/embed?group_id=' + group_id

无头浏览器

options.add_argument("--headless")

加一个伪装

options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"')

2、使用selenium访问该链接
driver: 浏览器

driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)

打开一个网页
驱动配置: 代码操作浏览器的一个中间人

driver.get(url)

隐式等待: 最多等待五秒 如果一秒钟加载完了 继续执行

driver.implicitly_wait(5)

3、提取视频链接地址

info = driver.find_elements_by_xpath('//*[@id="player_default"]/xg-controls/xg-definition/ul/li[1]')
video_url = info[0].get_attribute("url")

4、拼接视频链接地址

video_url = 'http:' + video_url

5、使用requests发送请求 并且获取视频二进制数据

video_data = requests.get(video_url).content
with open('1.mp4', mode='wb') as f:
    f.write(video_data)
import requests
from selenium import webdriver

# 进入浏览器设置
options = webdriver.ChromeOptions()
# 1. 构建embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238
group_id = input("请输入你要下载视频的id:")
url = 'https://www.ixigua.com/embed?group_id=' + group_id
# 无头浏览器
options.add_argument("--headless")
# 加一个伪装
options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"')
# 2. 使用selenium访问该链接
# driver: 浏览器
driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)
# 打开一个网页
# 驱动配置: 代码操作浏览器的一个中间人
driver.get(url)
# 隐式等待: 最多等待五秒 如果一秒钟加载完了 继续执行
driver.implicitly_wait(5)
# 3. 提取视频链接地址
info = driver.find_elements_by_xpath('//*[@id="player_default"]/xg-controls/xg-definition/ul/li[1]')
video_url = info[0].get_attribute("url")
# 4. 拼接视频链接地址
video_url = 'http:' + video_url
# 5. 使用requests发送请求 并且获取视频二进制数据
video_data = requests.get(video_url).content
with open('1.mp4', mode='wb') as f:
    f.write(video_data)
print("爬取成功!!!")
#留了报错,看看大家够不够机智找出来

兄弟们看完觉得有帮助,记得点赞三连哇~
在这里插入图片描述


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK