#!/usr/bin/env python
#encoding:utf-8
import requests
from bs4 import BeautifulSoup
import urllib
import sys
import re
# 解決編碼錯誤問題
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')) # 將查詢關鍵字進行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' # 設置相應體的字符集
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:
# 獲取結果標題
temptitle = str(table.find("a"))
temptitle = temptitle.replace("<font color=\"red\">","")
temptitle = temptitle.replace("</font>","")
temp = temptitle.split(">")
title = temp[1].split("<")[0]
# 獲取結果描述
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]
# 獲取頁面詳細地址
src = url + table.find("a")['href']
# 參考DOM節點 : 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 "單條數據獲取成功 !"
return results
# 首先根據第一個頁面獲取總頁面數
content = getContent(searchUrl)
soup = BeautifulSoup(content, "html.parser")
resultNumber = getResultNumber(soup)
pageNumber = getPageNumber(resultNumber)
print "查詢結果數 :", resultNumber
print "總頁面數量 :", pageNumber
print "正在獲取第 1 頁的結果"
results = getResultDic(soup)
print "該頁所有結果獲取成功 !"
for page in range(1, pageNumber):
print "正在獲取第",(page + 1),"頁的結果"
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 "該頁所有結果獲取成功 !"
# 格式化顯示數據 :
for result in results:
file = open(modelName + "-" + keyword + ".txt","a+")
file.write("---------------------------\n")
file.write("標題 : " + result['title'] + "\n")
file.write("描述 : \n\t" + result['describe'] + "\n")
file.write("下載地址 : \n\t" + result['downloadLink'] + "\n")
file.write("\n")
file.close()
[懶人福利]用Python進行[陽光電影網站]下載資源的搜索
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...