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

yipeiwu_com5年前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爬虫文件下载图文教程

Python爬虫文件下载图文教程

而今天我们要说的内容是:如果在网页中存在文件资源,如:图片,电影,文档等。怎样通过Python爬虫把这些资源下载下来。 1、怎样在网上找资源: 就是百度图片为例,当你如下图在百度图片里搜...

使用python爬虫获取黄金价格的核心代码

使用python爬虫获取黄金价格的核心代码

继续练手,根据之前获取汽油价格的方式获取了金价,暂时没钱投资,看看而已 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 获取每天黄...

python抓取京东商城手机列表url实例代码

复制代码 代码如下:#-*- coding: UTF-8 -*-'''Created on 2013-12-5 @author: good-temper''' import urllib...

使用Python爬取最好大学网大学排名

本文实例为大家分享了Python爬取最好大学网大学排名的具体代码,供大家参考,具体内容如下 源代码: #-*-coding:utf-8-*- ''''' Created on...

玩转python爬虫之正则表达式

玩转python爬虫之正则表达式

面对大量杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特...