1. 程式人生 > >python 爬取視訊 學習

python 爬取視訊 學習

程式碼中使用的BeautifulSoup 的select,也可使用 find和find_all

re.findall中req為正則表示式

urllib.request.urlretrieve()#將視訊複製入檔案中
import requests
import  re
import urllib.request
from bs4 import BeautifulSoup
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'}
req = re.compile(r'srcUrl="(.*?)"')
def download(url):

    soup=BeautifulSoup(requests.get(url,headers=headers).text,'html.parser')
    for i in soup.select('.popularem-ath'):
        a=i.select('a')[0]
        id=a['href']
        title=a.find(class_='popularem-title').text
        new_url='http://www.pearvideo.com/{}'.format(id)
        res = requests.get(new_url, headers=headers, timeout=3).text
        get_video=re.findall(req,res)[0]

        urllib.request.urlretrieve(get_video,'F:\Spider\VideoSpider\{}.mp4'.format(title))

if __name__=='__main__':
    url='http://www.pearvideo.com/popular_4'
    download(url)