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

yipeiwu_com4年前Python爬虫

复制代码 代码如下:

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爬虫项目设置一个中断重连的程序的实现

做爬虫项目时,我们需要考虑一个爬虫在爬取时会遇到各种情况(网站验证,ip封禁),导致爬虫程序中断,这时我们已经爬取过一些数据,再次爬取时这些数据就可以忽略,所以我们需要在爬虫项目中设置一...

Python爬虫实战:分析《战狼2》豆瓣影评

Python爬虫实战:分析《战狼2》豆瓣影评

刚接触python不久,做一个小项目来练练手。前几天看了《战狼2》,发现它在最新上映的电影里面是排行第一的,如下图所示。准备把豆瓣上对它的影评做一个分析。 目标总览 主要做了三件事:...

python爬虫(入门教程、视频教程) 原创

python的版本经过了python2.x和python3.x等版本,无论哪种版本,关于python爬虫相关的知识是融会贯通的,【听图阁-专注于Python设计】关于爬虫这个方便整理过很...

Python爬虫的两套解析方法和四种爬虫实现过程

Python爬虫的两套解析方法和四种爬虫实现过程

对于大多数朋友而言,爬虫绝对是学习 python 的最好的起手和入门方式。因为爬虫思维模式固定,编程模式也相对简单,一般在细节处理上积累一些经验都可以成功入门。本文想针对某一网页对&nb...

使用python 爬虫抓站的一些技巧总结

使用python 爬虫抓站的一些技巧总结

学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自动收邮件的脚本,写过简单的验证码识别的脚本,本...