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实现并行抓取整站40万条房价数据(可更换抓取城市)

Python实现并行抓取整站40万条房价数据(可更换抓取城市)

写在前面 这次的爬虫是关于房价信息的抓取,目的在于练习10万以上的数据处理及整站式抓取。 数据量的提升最直观的感觉便是对函数逻辑要求的提高,针对Python的特性,谨慎的选择数据结构。以...

python如何爬取网站数据并进行数据可视化

python如何爬取网站数据并进行数据可视化

前言 爬取拉勾网关于python职位相关的数据信息,并将爬取的数据已csv各式存入文件,然后对csv文件相关字段的数据进行清洗,并对数据可视化展示,包括柱状图展示、直方图展示、词云展示等...

python爬虫基本知识

爬虫简介       根据百度百科定义:网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照...

搞定这套Python爬虫面试题(面试会so easy)

搞定这套Python爬虫面试题(面试会so easy)

先来一份完整的爬虫工程师面试考点: 一、 Python 基本功 1、简述Python 的特点和优点 Python 是一门开源的解释性语言,相比 Java C++ 等语言,Python...

python爬虫入门教程之点点美女图片爬虫代码分享

继续鼓捣爬虫,今天贴出一个代码,爬取点点网「美女」标签下的图片,原图。 # -*- coding: utf-8 -*- #----------------------------...