Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法访问<;来源>;用BeautifulSoup标记_Python_Beautifulsoup - Fatal编程技术网

Python 无法访问<;来源>;用BeautifulSoup标记

Python 无法访问<;来源>;用BeautifulSoup标记,python,beautifulsoup,Python,Beautifulsoup,我正在构建一个脚本,使用请求和BeautifulSoup从指定的gyfycat网页下载.mp4文件。我遇到一个错误,无法访问源标记的'src'属性。我的目标是以下HTML元素: <source src="https://giant.gfycat.com/PoshDearAsianporcupine.mp4" type="video/mp4"> 这里的问题是不是每个source标记都有src属性,在本例中,第一个标记没有。您可以使用如下条件列表理解来收集所有src属性(如果存在):

我正在构建一个脚本,使用请求和BeautifulSoup从指定的gyfycat网页下载.mp4文件。我遇到一个错误,无法访问源标记的
'src'
属性。我的目标是以下HTML元素:

<source src="https://giant.gfycat.com/PoshDearAsianporcupine.mp4" type="video/mp4">

这里的问题是不是每个
source
标记都有
src
属性,在本例中,第一个标记没有。您可以使用如下条件列表理解来收集所有
src
属性(如果存在):

srcs = [tag["src"] for tag in sourceTags if "src" in tag.attrs]
结果:

['https://giant.gfycat.com/PoshDearAsianporcupine.webm', 'https://giant.gfycat.com/PoshDearAsianporcupine.mp4', 'https://thumbs.gfycat.com/PoshDearAsianporcupine-mobile.mp4']
['https://giant.gfycat.com/PoshDearAsianporcupine.webm', 'https://giant.gfycat.com/PoshDearAsianporcupine.mp4', 'https://thumbs.gfycat.com/PoshDearAsianporcupine-mobile.mp4']