如何提取“https://www.example.com/get_file/4/b315c8e0d6fad43d89445378b5292eed6981a999ba/174000/174469/174469.mp4/?br=707”和使用“video_url”
[<script src="https://www.example.com/player/kt_player.js?v=5.5.1" type="text/javascript"></script>, <script type="text/javascript">
/* <![CDATA[ */
function getEmbed(width, height) {
if (width && height) {
return '<iframe width="' width '" height="' height '" src="https://www.example.com/embed/174469" frameborder="0" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>';
}
return '<iframe width="852" height="480" src="https://www.example.com/embed/174469" frameborder="0" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>';
}
var flashvars = {
video_id: '174469', video_categories: 'example_category1', 'example_category2', video_tags: 'example_tag1', 'example_tag2', license_code: '$603825119921245', rnd: '1650848189', video_url: 'https://www.example.com/get_file/4/b315c8e0d6fad43d89445378b5292eed6981a999ba/174000/174469/174469.mp4/?br=707', postfix: '.mp4', video_url_text: '480p', video_alt_url: 'https://www.example.com/get_file/4/ffafbe6913656c2250c34bf20fd945a5f86898d749/174000/174469/174469_720p.mp4/?br=1290', video_alt_url_text: '720p', video_alt_url_hd: '1', video_alt_url2: 'https://www.example.com/get_file/4/66c8876a9fd8cd3d823d06880c1797b1424f3200df/174000/174469/174469_1080p.mp4/?br=2559', video_alt_url2_text: '1080p', video_alt_url2_hd: '1', preview_url: 'https://www.example.com/contents/videos_screenshots/174000/174469/preview.jpg', preview_url1: 'https://www.example.com/contents/videos_screenshots/174000/174469/preview.mp4.jpg', preview_height1: '480', preview_url2: 'https://www.example.com/contents/videos_screenshots/174000/174469/preview_720p.mp4.jpg', preview_height2: '720', preview_url3: 'https://www.example.com/contents/videos_screenshots/174000/174469/preview_1080p.mp4.jpg', preview_height3: '1080', skin: 'youtube.css', logo_position: '0,0', logo_anchor: 'topleft', hide_controlbar: '1', hide_style: 'fade', volume: '1', related_src: 'https://www.example.com/related_videos_html/174469/', adv_pre_vast: 'https://twinrdsrv.com/preroll.engine?id=613eb379-62dd-49ef-8299-db2b5b2af4d7&zid=12861&cvs={ClientVideoSupport}&time={TimeOffset}&stdtime={StdTimeOffset}&abr={IsAdblockRequest}&pageurl={PageUrl}&tid={TrackingId}&res={Resolution}&bw={BrowserWidth}&bh={BrowserHeight}&kw={Keywords}&referrerUrl={ReferrerUrl}&pw={PlayerWidth}&ph={PlayerHeight}', adv_pre_skip_duration: '5', adv_pre_skip_text_time: 'Skip ad in %time', adv_pre_skip_text: 'Skip ad', adv_post_vast: 'https://twinrdsrv.com/preroll.engine?id=613eb379-62dd-49ef-8299-db2b5b2af4d7&zid=12861&cvs={ClientVideoSupport}&time={TimeOffset}&stdtime={StdTimeOffset}&abr={IsAdblockRequest}&pageurl={PageUrl}&tid={TrackingId}&res={Resolution}&bw={BrowserWidth}&bh={BrowserHeight}&kw={Keywords}&referrerUrl={ReferrerUrl}&pw={PlayerWidth}&ph={PlayerHeight}', adv_post_skip_duration: '5', adv_post_skip_text_time: 'Skip ad in %time', adv_post_skip_text: 'Skip ad', lrcv: '1651572296480833989009946', vast_timeout1: '10', player_width: '882', player_height: '496.9014084507', embed: '1' };
var player_obj = kt_player('kt_player', 'https://www.example.com/player/kt_player.swf?v=5.5.1', '100%', '100%', flashvars);
window.onload = function() {
$('.pop-adv .btn').click(function(e) {
player_obj.play();
});
};
/* ]]> */
</script>]
我試過;
import json
script= """[<script src="https://www.example.com/player/kt_player.js?v=5.5.1" type="text/javascript"></script>, <script type="text/javascript">
/* <![CDATA[ */
function getEmbed(width, height) {
if (width && height) {
return '<iframe width="' width '" height="' height '" src="https://www.example.com/embed/174469" frameborder="0" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>';
}
return '<iframe width="852" height="480" src="https://www.example.com/embed/174469" frameborder="0" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>';
}
var flashvars = {
video_id: '174469', video_categories: 'example_category1, example_category2', video_tags: 'example_tag1, esample_tag2', license_code: '$603825119921245', rnd: '1650848189', video_url: 'https://www.example.com/get_file/4/b315c8e0d6fad43d89445378b5292eed6981a999ba/174000/174469/174469.mp4/?br=707', postfix: '.mp4', video_url_text: '480p', video_alt_url: 'https://www.example.com/get_file/4/ffafbe6913656c2250c34bf20fd945a5f86898d749/174000/174469/174469_720p.mp4/?br=1290', video_alt_url_text: '720p', video_alt_url_hd: '1', video_alt_url2: 'https://www.example.com/get_file/4/66c8876a9fd8cd3d823d06880c1797b1424f3200df/174000/174469/174469_1080p.mp4/?br=2559', video_alt_url2_text: '1080p', video_alt_url2_hd: '1', preview_url: 'https://www.example.com/contents/videos_screenshots/174000/174469/preview.jpg', preview_url1: 'https://www.example.com/contents/videos_screenshots/174000/174469/preview.mp4.jpg', preview_height1: '480', preview_url2: 'https://www.example.com/contents/videos_screenshots/174000/174469/preview_720p.mp4.jpg', preview_height2: '720', preview_url3: 'https://www.example.com/contents/videos_screenshots/174000/174469/preview_1080p.mp4.jpg', preview_height3: '1080', skin: 'youtube.css', logo_position: '0,0', logo_anchor: 'topleft', hide_controlbar: '1', hide_style: 'fade', volume: '1', related_src: 'https://www.example.com/related_videos_html/174469/', adv_pre_vast: 'https://twinrdsrv.com/preroll.engine?id=613eb379-62dd-49ef-8299-db2b5b2af4d7&zid=12861&cvs={ClientVideoSupport}&time={TimeOffset}&stdtime={StdTimeOffset}&abr={IsAdblockRequest}&pageurl={PageUrl}&tid={TrackingId}&res={Resolution}&bw={BrowserWidth}&bh={BrowserHeight}&kw={Keywords}&referrerUrl={ReferrerUrl}&pw={PlayerWidth}&ph={PlayerHeight}', adv_pre_skip_duration: '5', adv_pre_skip_text_time: 'Skip ad in %time', adv_pre_skip_text: 'Skip ad', adv_post_vast: 'https://twinrdsrv.com/preroll.engine?id=613eb379-62dd-49ef-8299-db2b5b2af4d7&zid=12861&cvs={ClientVideoSupport}&time={TimeOffset}&stdtime={StdTimeOffset}&abr={IsAdblockRequest}&pageurl={PageUrl}&tid={TrackingId}&res={Resolution}&bw={BrowserWidth}&bh={BrowserHeight}&kw={Keywords}&referrerUrl={ReferrerUrl}&pw={PlayerWidth}&ph={PlayerHeight}', adv_post_skip_duration: '5', adv_post_skip_text_time: 'Skip ad in %time', adv_post_skip_text: 'Skip ad', lrcv: '1651572296480833989009946', vast_timeout1: '10', player_width: '882', player_height: '496.9014084507', embed: '1' };
var player_obj = kt_player('kt_player', 'https://www.example.com/player/kt_player.swf?v=5.5.1', '100%', '100%', flashvars);
window.onload = function() {
$('.pop-adv .btn').click(function(e) {
player_obj.play();
});
};
/* ]]> */
</script>]"""
json_data= json.loads(script)
print(json_data['video_url'])
并得到這個錯誤,
json.decoder.JSONDecodeError:預期值:第 1 行第 2 列(字符 1)
uj5u.com熱心網友回復:
這不是一個理想的解決方案,因為它依賴于具有非常一致格式的源檔案,但您可以嘗試使用正則運算式“手動”決議它:
以下假設您已設定script為包含上面下載的文本。
import re
def extract(name, script):
return re.search(rf'\b{name}\s*:\s*(\'|")(.*?)\1', script)[2]
extract('video_url', script)
'https://www.example.com/get_file/4/b315c8e0d6fad43d89445378b5292eed6981a999ba/174000/174469/174469.mp4/?br=707'
extract('video_alt_url', script)
'https://www.example.com/get_file/4/ffafbe6913656c2250c34bf20fd945a5f86898d749/174000/174469/174469_720p.mp4/?br=1290'
extract('video_alt_url2', script)
'https://www.example.com/get_file/4/66c8876a9fd8cd3d823d06880c1797b1424f3200df/174000/174469/174469_1080p.mp4/?br=2559'
正則運算式的作業方式如下:
\b{name}\s*:\s*匹配name:允許可變間距的零件('|")(.*?)\1匹配一個'some_text'或"some_text"樣式字串[2]末尾的取第二組,即匹配(.*?)引號內的文本
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/463831.html
標籤:javascript Python 网页抓取 美丽的汤
