搜索电影资源小助手

女朋友说经常找不到电影资源看,最近准备离职比较闲,所以做了一个小助手。

版本1:只有搜索电影磁力链接的功能,慢慢增加功能

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 30 17:01:26 2018

@author: gzs10227

搜索电影资源
"""

import re,os
import requests
import time,datetime
import urllib
import sys
stderr = sys.stderr
stdout = sys.stdout
reload(sys)
sys.setdefaultencoding('utf8')
sys.stderr = stderr
sys.stdout = stdout
urllib.getproxies_registry = lambda: {}
null = ''
from lxml import etree
import locale

HEADERS = {
    'X-Requested-With': 'XMLHttpRequest',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
                  '(KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}

print u'请输入你想搜索的电影:'
keyword = raw_input().decode(sys.stdin.encoding or locale.getpreferredencoding(True)) 

print u'\n请输入你想保存文件的路径:比如 E:\电影搜索'
save_path = raw_input().decode(sys.stdin.encoding or locale.getpreferredencoding(True))
save = os.path.join(save_path+'\\',keyword+'.txt')
fle = open(save,'w')


def open_url(url):
    html = requests.get(url,headers = HEADERS).content
    web_data = etree.HTML(html)
    return web_data


def get_url(keyword):
    main_url = 'http://www.btyunsou.me/search/%s_ctime_1.html'%keyword
    web_data = open_url(main_url)
    links = web_data.xpath('//li[@class="media"]//h4//a/@href')          # 获取链接
    links = ['http://www.btyunsou.me'+i for i in links]
    return links


def get_info(url):    
    web_data = open_url(url)
    try:
        title = web_data.xpath(r'//div[@class="row-flbtd tor-title"]/h2/text()')[0]
    except:
        title = ''
    if keyword in title:        
        print u'电影名:',title
        mange_link = 'magnet:?xt=urn:btih:' + url[23:-5]
        print u'磁力链接: ',mange_link
        fle.write(u'电影名:'+ title + '\n')
        fle.write(u'磁力链接: ' + mange_link +'\n')
        datalist = web_data.xpath(r'//table[@class="table detail table-hover"]/tbody//tr//td/text()')[:10]
        for i in range(0,len(datalist),2):
            print datalist[i],datalist[i+1]
            fle.write(datalist[i] + datalist[i+1] + '\n')
    else:
        print 'Sorry! None Search,Please change one: '


if __name__ == '__main__':       
    i = 1
    while True:    
        if i > 1:
            print u'请输入你想搜索的电影:'
            keyword = raw_input().decode(sys.stdin.encoding or locale.getpreferredencoding(True)) 
            save = os.path.join(save_path,keyword+'.txt')
            fle = open(save,'w')
        links = get_url(keyword)
        for url in links:            
            get_info(url)
            fle.write('--------------------------------------------------')
            fle.write('\n')
        fle.close()
        i = i + 1
        print u'\n如果您想再次搜索,请输入电影名!否则请手动关闭窗口.\n'
    

配置文件:
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 30 19:27:06 2018


@author: gzs10227
"""


from distutils.core import setup  
import py2exe  
import sys
sys.setrecursionlimit(5000)
setup(console=["search_movie.py"],
     options={
        'py2exe': 
        {
            'includes': ['lxml.etree', 'lxml._elementpath', 'gzip'],
        }
    }
) 
    
需要安装py2exe,然后在cmd中输入 python mysetup.py py2exe就可以了。
    
    


你可能感兴趣的:(python爬虫那些坑)