[懶人福利]用Python進(jìn)行[陽光電影網(wǎng)站]下載資源的搜索

#!/usr/bin/env python
#encoding:utf-8

import requests
from bs4 import BeautifulSoup
import urllib
import sys
import re

# 解決編碼錯(cuò)誤問題
reload(sys)  
sys.setdefaultencoding('utf8') 

'''
陽光電影模塊
'''

# config-start
modelName = "陽光電影"
url = "http://www.ygdy8.com"
keyword = sys.argv[1]
keywordURLencode = urllib.quote(keyword.decode(sys.stdin.encoding).encode('gbk')) # 將查詢關(guān)鍵字進(jìn)行URL編碼
searchUrl = "http://s.dydytt.net/plus/search.php?kwtype=0&searchtype=title&keyword=" + keywordURLencode
# config-end

def getContent(url):
    headers = {
        'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding' : 'gzip, deflate, sdch',
        'Accept-Language' : 'zh-CN,zh;q=0.8,en;q=0.6',
        'Cache-Control' : 'max-age=0',
        'Cookie' : 'PHPSESSID=075a0c375495ba59fdb2b8d3d3280113',
        'Host' : 's.dydytt.net',
        'Proxy-Connection' : 'keep-alive',
        'Referer' : 'http://www.ygdy8.com/',
        'Upgrade-Insecure-Requests' : '1',
        'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
    }
    response = requests.get(url)
    response.encoding = 'gb2312' # 設(shè)置相應(yīng)體的字符集
    return response.text


def getResultNumber(soup):
    td = soup.find("td",width="50")
    a = td.find("a")['href']
    totalResult = str(a).split("&")[-2]
    return int(totalResult.split("=")[1])

def getPageNumber(resultNumber):
    return int((resultNumber / 10)) + 1

def getResultDic(soup):
    results = []
    tables = soup.findAll("table", border='0', width='100%')[1:]
    for table in tables:
        # 獲取結(jié)果標(biāo)題
        temptitle = str(table.find("a"))
        temptitle = temptitle.replace("<font color=\"red\">","")
        temptitle = temptitle.replace("</font>","")
        temp = temptitle.split(">")
        title = temp[1].split("<")[0]
        # 獲取結(jié)果描述
        tempdescribe = str(table.find("td", colspan="3", height="56"))
        tempdescribe = tempdescribe.replace("<font color=\"red\">","")
        tempdescribe = tempdescribe.replace("</font>","")
        temp = tempdescribe.split(">")
        describe = temp[1].split("<")[0]
        # 獲取頁面詳細(xì)地址
        src = url + table.find("a")['href']
        # 參考DOM節(jié)點(diǎn) : td style="WORD-WRAP: break-word" bgcolor="#fdfddf"
        newContent = getContent(src)
        newSoup = BeautifulSoup(newContent, "html.parser")
        newtd = newSoup.find("td", style="WORD-WRAP: break-word", bgcolor="#fdfddf")
        downloadLink = newtd.find("a")['href']
        result = {
            "title":title,
            "describe":describe,
            "downloadLink":downloadLink
        }
        results.append(result)
        print "單條數(shù)據(jù)獲取成功 !"
    return results

# 首先根據(jù)第一個(gè)頁面獲取總頁面數(shù)
content = getContent(searchUrl)
soup = BeautifulSoup(content, "html.parser")
resultNumber = getResultNumber(soup)
pageNumber = getPageNumber(resultNumber)


print "查詢結(jié)果數(shù) :", resultNumber
print "總頁面數(shù)量 :", pageNumber

print "正在獲取第 1 頁的結(jié)果" 
results = getResultDic(soup)
print "該頁所有結(jié)果獲取成功 !"

for page in range(1, pageNumber):
    print "正在獲取第",(page + 1),"頁的結(jié)果" 
    searchUrl = "http://s.dydytt.net/plus/search.php?kwtype=0&searchtype=title&keyword=" + keyword + "&PageNo=" + str(page + 1)
    content = getContent(searchUrl)
    soup = BeautifulSoup(content, "html.parser")
    results += getResultDic(soup)
    print "該頁所有結(jié)果獲取成功 !"

# 格式化顯示數(shù)據(jù) : 
for result in results:
    file = open(modelName + "-" + keyword + ".txt","a+")
    file.write("---------------------------\n")
    file.write("標(biāo)題 : " + result['title'] + "\n")
    file.write("描述 : \n\t" + result['describe'] + "\n")
    file.write("下載地址 : \n\t" + result['downloadLink'] + "\n")
    file.write("\n")
    file.close()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容