使用urllib和bs4下载的视频无法播放

时间:2016-10-31 05:26:01

标签: python django urllib2 urllib bs4

我有一个在线文件夹,我有视频文件,所以我需要从那里下载视频到我的本地系统,所以为了做到这一点我使用 Bs4 urllib 模块,下载的文件无法播放

请查看我的代码。



from bs4 import BeautifulSoup
import urllib2
import random
import urllib
from urllib2 import urlopen
         
def download(url):
    response = urllib.urlopen("http://ssdolutions/addadsfasdfulsdfaatadfae")
    doc  = response.read()
    soup = BeautifulSoup(doc)
    for link in soup.find_all('a'):
        x = (link.get('href'))
        name = random.randrange(1,10)
        full_name = str(name) + ".mp4"
        urllib.urlretrieve(url, full_name)
download("http://ssdolutions/addadsfasdfulsdfaatadfae")




1 个答案:

答案 0 :(得分:0)

将每个链接网址(存储在x变量中)传递给urlretrieve来电:

from bs4 import BeautifulSoup
import urllib2
import random
import urllib
import os
from urllib2 import urlopen

def download(url):
    response = urllib.urlopen("http://ssdolutions/addadsfasdfulsdfaatadfae")
    doc  = response.read()
    soup = BeautifulSoup(doc)
    for link in soup.find_all('a'):
        url = (link.get('href'))
        # Extract filename from link URL
        filename = os.path.basename(url)
        file_data = os.path.splitext(filename)
        if len(file_data) > 1:
           file_ext = file_data[1]
           # this will allow you to download links with *.mp4 extension only
           if file_ext == ".mp4":
              urllib.urlretrieve(url, filename)

download("http://ssdolutions/addadsfasdfulsdfaatadfae")