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爬虫 urllib模块发起post请求过程解析

python爬虫 urllib模块发起post请求过程解析

urllib模块发起的POST请求 案例:爬取百度翻译的翻译结果 1.通过浏览器捉包工具,找到POST请求的url 针对ajax页面请求的所对应url获取,需要用到浏览器的捉包工具。...

利用python-pypcap抓取带VLAN标签的数据包方法

利用python-pypcap抓取带VLAN标签的数据包方法

1、背景介绍 在采用通常的socket抓包方式下,操作系统会自动将收到包的VLAN信息剥离,导致上层应用收到的包不会含有VLAN标签信息。而libpcap虽然是基于socket实现抓包,...

Scrapy抓取京东商品、豆瓣电影及代码分享

 1.scrapy基本了解   Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。可以应用在包括数据挖掘, 信息处理或存储历史数据等一系列的程序中。其最初是为了页面抓取...

python爬虫URL重试机制的实现方法(python2.7以及python3.5)

应用场景: 状态不是200的URL重试多次 代码比较简单还有部分注释 python2.7实现: # -*-coding:utf-8-*- """ ayou """ import...

解决Python3 抓取微信账单信息问题

这段时间有个朋友想导出微信里面的账单信息,后来发现微信的反爬虫还是很厉害的,花了点时间去分析。 一、采用传统模拟http抓取 抓取的主要URL:https://wx.tenpay.com...