美丽的汤 - 查找儿童标签属性内容

时间:2018-01-27 16:06:24

标签: python html web-scraping beautifulsoup

源代码:

<div class="wrapper">
    <div id="mask" style="display: none;"></div>
    <div id="video">
        <span id="pid" hidden="">2</span>
        <div poster="https://thumbs.vodgc.net/57377706F7D28069F41A23A14DC5CC64.jpg?673333" autoplay="true" data-setup="{ &quot;techOrder&quot;: [&quot;html5&quot;]}"
            preload="none" class="video-js vjs-default-skin vjs-controls-enabled vjs-workinghover vjs-has-started media_player-dimensions vjs-paused vjs-user-inactive"
            id="media_player" role="region" aria-label="video player">
            <video id="media_player_html5_api" class="vjs-tech" preload="none" data-setup="{ &quot;techOrder&quot;: [&quot;html5&quot;]}"
                autoplay="" src="blob:https://api.vodgc.net/5bb5a7a7-6c9b-49f1-883b-784871f95d8b">
                <source src="https://vod.vodgc.net/manifest/57377706F7D28069F41A23A14DC5CC64.m3u8" type="application/x-mpegURL">
            </video>
            <div>

我试图在'source'标签中找到'src属性的内容,但我一直得到一个无效的结果或空列表。

这是我的代码:

from urllib import request
from bs4 import BeautifulSoup

hdr = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
       'Accept': 
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

url = 'https://www.eltrecetv.com.ar/programas/simona/capitulos-completos/capitulo-4_099474'
req = request.Request(url, headers=hdr)

page = request.urlopen(req)

soup = BeautifulSoup(page,'lxml')


sources = soup.find('div', class_ ='wrapper')

for tag in sources:
    video = tag.find_next_siblings('video')
    print(video)

1 个答案:

答案 0 :(得分:2)

通过将src标记传递给source方法来访问find_all属性:

from bs4 import BeautifulSoup as soup

s = """
<div class="wrapper">
<div id="mask" style="display: none;"></div>
<div id="video">
    <span id="pid" hidden="">2</span>
    <div poster="https://thumbs.vodgc.net/57377706F7D28069F41A23A14DC5CC64.jpg?673333" autoplay="true" data-setup="{ &quot;techOrder&quot;: [&quot;html5&quot;]}"
        preload="none" class="video-js vjs-default-skin vjs-controls-enabled vjs-workinghover vjs-has-started media_player-dimensions vjs-paused vjs-user-inactive"
        id="media_player" role="region" aria-label="video player">
        <video id="media_player_html5_api" class="vjs-tech" preload="none" data-setup="{ &quot;techOrder&quot;: [&quot;html5&quot;]}"
            autoplay="" src="blob:https://api.vodgc.net/5bb5a7a7-6c9b-49f1-883b-784871f95d8b">
            <source src="https://vod.vodgc.net/manifest/57377706F7D28069F41A23A14DC5CC64.m3u8" type="application/x-mpegURL">
        </video>
        <div>
"""
d = soup(s, 'lxml')
print([i['src'] for i in d.find_all('source')])

输出:

['https://vod.vodgc.net/manifest/57377706F7D28069F41A23A14DC5CC64.m3u8']