BeautifulSoup 找不到视频或某些 div 标签

BeautifulSoup can't find video or certain div tags

我不知道为什么它不允许我访问视频标签。

我正在尝试抓取视频源,但它根本不允许我访问 'video' 标签。

<video class="jw-video jw-reset" disableremoteplayback="" webkit-
     playsinline="" playsinline="" jw-loaded="data" 
     src="randomsrc2" jw-played="" style="object-fit: 
     fill;"></video>

    #web scraping stuff
    #web scraping stuff
    import bs4 as bs
    import urllib.request

    url = 'https://gostream.is/film/cars-3-21095/watching.html?ep=682669'
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; 
    rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'

    headers={'User-Agent':user_agent,}

    q = urllib.request.Request(url, headers=headers)
    sauce = urllib.request.urlopen(q).read()
    soup = bs.BeautifulSoup(sauce,'lxml')
    print(soup)

    f=open('testd2.txt','w+')
    kuk = str(soup)
    f.write(kuk) #When I search for 'video' in the file it doesn't give me anything
    video = soup.find('video')
    print(video) #gives None

在 firefox 中转到 about:config 并搜索 javascript.enabled 为 false。打开你的link。如果您在浏览器中没有看到您的视频 link,则表示正在使用 JavaScript 在 运行 时插入标签。而请求将无法做到这一点。

为此,您需要有浏览器和 selenium。在这种情况下,您将有机会获得如下代码

from selenium import webdriver
driver = webdriver.Firefox()
url = 'https://gostream.is/film/cars-3-21095/watching.html?ep=682669'
driver.get(url)
sauce = driver.page_source
soup = bs.BeautifulSoup(sauce,'lxml')

你甚至可以把汤一起去掉,然后用下面的东西

for elem in driver.find_elements_by_tag_name("video"):
    print(elem.get_attribute("src"))