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爬虫之线程池和进程池功能与用法。分享给大家供大家参考,具体如下: 一、需求 最近准备爬取某电商网站的数据,先不考虑代理、分布式,先说效率问题(当然你要是请求的太...

python解决网站的反爬虫策略总结

本文详细介绍了网站的反爬虫策略,在这里把我写爬虫以来遇到的各种反爬虫策略和应对的方法总结一下。 从功能上来讲,爬虫一般分为数据采集,处理,储存三个部分。这里我们只讨论数据采集部分。 一...

零基础写python爬虫之使用Scrapy框架编写爬虫

零基础写python爬虫之使用Scrapy框架编写爬虫

网络爬虫,是在网上进行数据抓取的程序,使用它能够抓取特定网页的HTML数据。虽然我们利用一些库开发一个爬虫程序,但是使用框架可以大大提高效率,缩短开发时间。Scrapy是一个使用Pyth...

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

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

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

Python微医挂号网医生数据抓取

Python微医挂号网医生数据抓取

1. 写在前面 今天要抓取的一个网站叫做微医网站,地址为 https://www.guahao.com/ ,我们将通过python3爬虫抓取这个网址,然后数据存储到CSV里面,为后面的一...