欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python使用beautifulsoup从爱奇艺网抓取视频播放

程序员文章站 2023-11-20 15:40:16
复制代码 代码如下:import sysimport urllibfrom urllib import requestimport osfrom bs4 import be...

复制代码 代码如下:

import sys
import urllib
from urllib import request
import os
from bs4 import beautifulsoup

class dramaitem:
    def __init__(self, num, title, url):
        self.num = num
        self.title = title
        self.url = url
    def __str__(self):
        return self.num + '    ' + self.title
    def opendrama(self):
        os.startfile(self.url)

response = urllib.request.urlopen('http://www.iqiyi.com/a_19rrgja8xd.html')
html = response.read()
soup = beautifulsoup(html)
dramalist = soup.findall('div', attrs={'class':'list_block1 align_c'})
dramaitems = []

if(dramalist):
    lis = dramalist[0].findall('li')
    for li in lis:
        ps = li.findall('p')
        description = ps[1].text if len(ps)>1 else ''
        num = ps[0].find('a').text
        url = ps[0].find('a')['href']
        di = dramaitem(num, description, url)
        dramaitems.append(di)

for di in dramaitems:
    print(di)
dilen = len(dramaitems)
userchoice = int(input('input number to watch the drama:'))
if userchoice >= 1 and userchoice <=dilen:
    dramaitems[userchoice-1].opendrama()



python使用beautifulsoup从爱奇艺网抓取视频播放