python爬虫-用beautifulsoup爬取爱奇艺视频

代码如下:

import sysimport urllibfrom urllib import requestimport osfrom bs4 import beautifulsoupclass 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(‘p’, 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