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爬虫 Pyppeteer使用方法解析

引言 Selenium 在被使用的时候有个麻烦事,就是环境的相关配置,得安装好相关浏览器,比如 Chrome、Firefox 等等,然后还要到官方网站去下载对应的驱动,最重要的还需要安...

python实现爬虫统计学校BBS男女比例之数据处理(三)

python实现爬虫统计学校BBS男女比例之数据处理(三)

本文主要介绍了数据处理方面的内容,希望大家仔细阅读。 一、数据分析 得到了以下列字符串开头的文本数据,我们需要进行处理 二、回滚 我们需要对httperror的数据进行再处理 因为代...

Python3直接爬取图片URL并保存示例

有时候我们会需要从网络上爬取一些图片,来满足我们形形色色直至不可描述的需求。 一个典型的简单爬虫项目步骤包括两步:获取网页地址和提取保存数据。 这里是一个简单的从图片url收集图片的例子...

零基础写python爬虫之抓取百度贴吧代码分享

这里就不给大家废话了,直接上代码,代码的解释都在注释里面,看不懂的也别来问我,好好学学基础知识去! 复制代码 代码如下: # -*- coding: utf-8 -*- #-------...

python实现从web抓取文档的方法

本文实例讲述了Python实现从Web的一个URL中抓取文档的方法,分享给大家供大家参考。具体方法分析如下: 实例代码如下: import urllib doc = urllib....