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

yipeiwu_com6年前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爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例

Python爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例

本文实例讲述了Python爬虫框架scrapy实现downloader_middleware设置proxy代理功能。分享给大家供大家参考,具体如下: 一、背景: 小编在爬虫的时候肯定会遇...

Python使用正则表达式抓取网页图片的方法示例

本文实例讲述了Python使用正则表达式抓取网页图片的方法。分享给大家供大家参考,具体如下: #!/usr/bin/python import re import urllib #获...

Python视频爬虫实现下载头条视频功能示例

Python视频爬虫实现下载头条视频功能示例

本文实例讲述了Python视频爬虫实现下载头条视频功能。分享给大家供大家参考,具体如下: 一、需求分析 抓取头条短视频 思路: 分析网页源码,查找解析出视频资源url(查看源代码,搜mp...

python实现的一只从百度开始不断搜索的小爬虫

文中用到了BeautifulSoup这个库, 目的是处理html文档分析的, 因为我只是提取了title的关键字,所以可以用正则表达式代替, 还有一个库是jieba, 这个库是中文分词的作用, 再有一...

零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers

零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers

在开始后面的内容之前,先来解释一下urllib2中的两个个方法:info / geturl urlopen返回的应答对象response(或者HTTPError实例)有两个很...