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框架Scrapy爬取自己的博客内容过程详解

基于python框架Scrapy爬取自己的博客内容过程详解

前言 python中常用的写爬虫的库常有urllib2、requests,对于大多数比较简单的场景或者以学习为目的,可以用这两个库实现。这里有一篇我之前写过的用urllib2+Beaut...

利用Python写一个爬妹子的爬虫

利用Python写一个爬妹子的爬虫

前言 最近学完Python,写了几个爬虫练练手,网上的教程有很多,但是有的已经不能爬了,主要是网站经常改,可是爬虫还是有通用的思路的,即下载数据、解析数据、保存数据。下面一一来讲。 1...

python爬虫 基于requests模块发起ajax的get请求实现解析

python爬虫 基于requests模块发起ajax的get请求实现解析

基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面...

Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容

Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容

1、引言 在Python网络爬虫内容提取器一文我们详细讲解了核心部件:可插拔的内容提取器类gsExtractor。本文记录了确定gsExtractor的技术路线过程中所做的编程实验。这是...

python爬虫入门教程--快速理解HTTP协议(一)

python爬虫入门教程--快速理解HTTP协议(一)

前言 爬虫的基本原理是模拟浏览器进行 HTTP 请求,理解 HTTP 协议是写爬虫的必备基础,招聘网站的爬虫岗位也赫然写着熟练掌握HTTP协议规范,写爬虫还不得不先从HTTP协议开始讲...