美丽汤找不到视频或某些div标签



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

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

<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。打开您的链接。如果您在浏览器中没有看到视频链接,则表示该标签是在运行时使用 JavaScript 插入的。请求将无法做到这一点。

为此,您需要有一个浏览器和硒。在这种情况下,您将有机会编写如下代码

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"))

最新更新