好的,我在这个问题上挠头已经太久了。我正在尝试使用Beautiful Soup检索网页上嵌入式视频的url,并请求使用Python 2.7.6中的模块。我检查了chrome中的html,可以看到视频的url,但是当我使用请求获取页面并使用Beautiful Soup时,找不到“ video”节点。从源头上看,视频窗口看起来是一个嵌套的html文档。我到处搜索,找不到为什么我无法检索到它。如果有人能指出正确的方向,我将不胜感激。谢谢。
这是其中一部影片的网址:
http://www.growingagreenerworld.com/episode125/
python大神给出的解决方案
问题是存在一个带有iframe
标记的video
,该标记在浏览器中异步加载。
好消息是,您可以通过向iframe
URL发出附加请求,将当前页面URL作为Referer
传递,来模拟这种行为。
实现方式:
import re
from bs4 import BeautifulSoup
import requests
url = 'http://www.growingagreenerworld.com/episode125/'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36'}
with requests.Session() as session:
session.headers = headers
response = session.get(url)
soup = BeautifulSoup(response.content)
# follow the iframe url
response = session.get('http:' + soup.iframe['src'], headers={'Referer': url})
soup = BeautifulSoup(response.content)
# extract the video URL from the script tag
print re.search(r'"url":"(.*?)"', soup.script.text).group(1)
印刷品:
http://pdl.vimeocdn.com/43109/378/290982236.mp4?token2=1424891659_69f846779e96814be83194ac3fc8fbae&aksessionid=678424d1f375137f