Selenium:Python爬虫进阶

jupiter
2022-08-31 / 0 评论 / 816 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年08月31日,已超过616天没有更新,若内容或图片失效,请留言反馈。

1.简介

1.1 什么是Selenium?

官网: Selenium是一个用于Web应用程序测试的工具。
真实:大量用于网络爬虫,相比requests爬虫,完全模拟真人使用浏览器的流程,对于动态JS加载的网页更容易爬取

1.2 Selenium的功能

  • 框架底层使用JavaScript模拟真实用户对浏览器进行操作。测试脚本执行时,浏览器自动按照脚本代码做出点击,输入,打开,验证等操作,就像真实用户所做的一样,从终端用户的角度测试应用程序。

    • 可用于较难的爬虫:动态JS加载、登录验证、表单提交等
  • 使用简单,可使用Python、Java等多种语言编写用例脚本。

1.3 为什么要学习Selenium?

  • requests爬虫局限性较大,分析困难、被封禁概率高
  • 可用于较难的爬虫

    • 伪装成真实的浏览器,被封禁的概率更低
    • 动态JS加载
    • 登录验证
    • 表单提交等

1.4 Selenium的缺点

  • 相比requests,性能比较差,爬取的慢

1.5 Selenium运行框架

image-20220619141850207

2.Selenium环境搭建

1.电脑安装谷歌Chrome浏览器(其他浏览器不推荐)

  • 需要看一下当前的Chrome版本号,下载对应ChromeDriver

2.下载安装 ChromeDriver

3.Python安装selenium库

pip install selenium

3.Selenium实战案例

3.1 爬取电影天堂的视频真实下载地址

用到selenium加载页面渲染出m3u8文件的地址

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from bs4 import BeautifulSoup
import re

# 实例化一个浏览器对象(传入浏览器的驱动程序) 通过Options实现无可视化界面的操作
chrome_options = Options()
chrome_options.add_argument('--headless') # 不显示浏览器在后台运行
chrome_service = Service("chromedriver.exe");
browser = webdriver.Chrome(service=chrome_service,options=chrome_options)

# 封装通过selenium获取html的方法
def get_html_by_selenium(url):
    # 对url发起请求
    browser.get(url)
    # 等待页面加载完成
    wait = WebDriverWait(browser, 3);
    # 获取页面源代码
    page_html = browser.page_source
    return page_html

# 电视剧页面地址_base
base_url = "https://www.dy10000.com/wplay/68599-2-{}.html"

# 生成下载脚本
for i in range(42,48):
    url = base_url.format(i)
    html = get_html_by_selenium(url)
    soup = BeautifulSoup(html, features='lxml')
    script_all = soup.body.find_all("script")
    for script in script_all:
        m3u8_search = re.search(r"http.*?m3u8", str(script))
        if m3u8_search:
            url_m3u8 = m3u8_search.group(0).replace("\\", "")
            print("ffmpeg -i ", url_m3u8, " -c copy -bsf:a aac_adtstoasc " + str(i) + ".mp4")
            break

# 退出浏览器
browser.quit()
ffmpeg -i  https://new.iskcd.com/20220518/FhKxDhXk/index.m3u8  -c copy -bsf:a aac_adtstoasc 42.mp4
ffmpeg -i  https://new.iskcd.com/20220518/2MSrEhUz/index.m3u8  -c copy -bsf:a aac_adtstoasc 43.mp4
ffmpeg -i  https://new.iskcd.com/20220519/7o5nJxJ3/index.m3u8  -c copy -bsf:a aac_adtstoasc 44.mp4
ffmpeg -i  https://new.iskcd.com/20220519/BB2x9BaG/index.m3u8  -c copy -bsf:a aac_adtstoasc 45.mp4
ffmpeg -i  https://new.iskcd.com/20220520/AxB2XF4T/index.m3u8  -c copy -bsf:a aac_adtstoasc 46.mp4

3.2 爬取电影先生的视频真实下载地址

用到selenium加载页面渲染出m3u8文件的地址

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from bs4 import BeautifulSoup
import re

# 实例化一个浏览器对象(传入浏览器的驱动程序) 通过Options实现无可视化界面的操作
chrome_options = Options()
chrome_options.add_argument('--headless') # 不显示浏览器在后台运行
chrome_service = Service("chromedriver.exe");
browser = webdriver.Chrome(service=chrome_service,options=chrome_options)

# 封装通过selenium获取html的方法
def get_html_by_selenium(url):
    # 对url发起请求
    browser.get(url)
    # 等待页面加载完成
    wait = WebDriverWait(browser, 5);
    # 获取页面源代码
    page_html = browser.page_source
    return page_html

# 电视剧页面地址_base
base_url = "https://dyxs15.com/paly-222156-5-{}/"

# 生成下载脚本
for i in range(15,41):
    url = base_url.format(i)
    html = get_html_by_selenium(url)
    soup = BeautifulSoup(html, features='lxml')
    td_all = soup.body.find_all("td")
    for td in td_all:
        m3u8_search = re.search(r"http.*?m3u8", str(td))
        if m3u8_search:
            url_m3u8 = m3u8_search.group(0).replace("\\", "")
            print("ffmpeg -i ", url_m3u8, " -c copy -bsf:a aac_adtstoasc " + str(i) + ".mp4")
            break

    break

# 退出浏览器
browser.quit()
ffmpeg -i  https://new.qqaku.com/20220526/t5C1cVna/index.m3u8  -c copy -bsf:a aac_adtstoasc 15.mp4
······
ffmpeg -i  https://new.qqaku.com/20220617/Wpf7uowm/index.m3u8  -c copy -bsf:a aac_adtstoasc 40.mp4

参考资料

  1. https://www.bilibili.com/video/BV1WF411z7qB
  2. 自动化爬虫selenium基础教程
  3. selenium如何不显示浏览器在后台运行
  4. Python selenium的这三种等待方式一定要会!
0

评论 (0)

打卡
取消