Python脚本-拉取最新上映电影免费看

  这是从id97拉取新上映电影的python脚本,很简单,如果需要拉取其它电影直接改一下正则表达式就可以了,程序测试环境:Win7 64位,Python 2.7。

  代码如下:

# -*- coding: utf-8 -*-

import urllib2;
import os;
import re;

# 找到电影
# 
def getVideoList():
    url = 'http://www.id97.com/';
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
    headers = { 'User-Agent' : user_agent }
    req = urllib2.Request(url, headers = headers)
    result = urllib2.urlopen(req);
    # print 'myResponse == ',myResponse;
    content = result.read();
    items = re.findall('',content,re.S);
    filePath = 'freefilm.txt';
    if os.path.exists(filePath):
        os.remove(filePath);
    f = open(filePath,'w+');
    for item in items:
        itemUrl = 'http://www.id97.com/videos/resource/id/' + item[0];
        onlineUrl = getVideoInfo(itemUrl);
        itemName = item[2];
        print 'itemName == ',itemName,' onlineUrl == ',onlineUrl;
        f.write('电影名称:' + itemName + ( 60 - len( itemName ) )*' ' +  '播放地址:' + onlineUrl + '\n' );
    f.close();

# 找播放地址
# 在线观看
def getVideoInfo(url):
    resultUrl = '';
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
    headers = { 'User-Agent' : user_agent }
    req = urllib2.Request(url, headers = headers)
    result = urllib2.urlopen(req);
    page = result.read();
    content = page.decode("utf-8");
    urls = re.findall('(.+?)'\
        ,content,re.S);
    for onlineUrl in urls:
        resultUrl = onlineUrl[0];
        break;
    return str(resultUrl);

# 拉取id97新上映电影
getVideoList();

  拉取结果如下:

电影名称:"霍比特人3:五军之战,高清在线播放" 播放地址:http://www.id97.com/videos/play/mid/8549.html

电影名称:"饥饿游戏3:嘲笑鸟(上).高清在线观看" 播放地址:http://www.id97.com/videos/play/mid/8548.html

电影名称:"爆裂鼓手,高清在线播放" 播放地址:http://www.id97.com/videos/play/mid/8543.html

电影名称:"生命之书,高清在线播放" 播放地址:http://www.id97.com/videos/play/mid/8518.html

电影名称:"马达加斯加的企鹅,高清在线播放" 播放地址:http://www.id97.com/videos/play/mid/8501.html

你可能感兴趣的:(Python脚本-拉取最新上映电影免费看)