title: 國外最新高清pdf尋找以及實現(xiàn)迅雷自動下載【Python】
date: 2016-10-11 20:59:28
tags:
國外最新高清pdf尋找以及實現(xiàn)迅雷自動下載#
1、今天意外發(fā)現(xiàn)國外某站流椒,提供非常近期敏簿,甚至國內(nèi)亞馬遜還沒上市的最新高清pdf,所以測試爬蟲宣虾,看是否能自動下載惯裕。
2、
《OReilly.Introduction.to.Machine.Learning.with.Python.A.Guide.for.Data.Scientists.1449369413》
一開始人工下載成功绣硝, 國內(nèi)要月底才上線呢蜻势。
3、 隨后測試程序是否可自動下載鹉胖,第二本書的下載遇到了問題:總提示服務(wù)器維護握玛,但更換了ip也這樣的結(jié)果,后發(fā)現(xiàn)是對應(yīng)網(wǎng)盤異常了甫菠。
4挠铲、代碼:
# -*- coding: utf-8 -*-
# python 3.5.2
# 測試系統(tǒng),Win10寂诱,F(xiàn)irefox V46
# Author:Van
# 實現(xiàn)自動下載高清最新pdf的實現(xiàn)
# V1.0 當(dāng)前只針對效果還可以的國外zippyshare網(wǎng)盤
# 其他的網(wǎng)盤還沒添加進判斷語句拂苹,先共享如何迅雷下載等
# 如果您有經(jīng)驗優(yōu)化,改進此腳本痰洒,請不吝指教
# QQ群: 206241755
# 簡介:因下載最新高清pdf瓢棒,正好發(fā)現(xiàn)www.foxebook.net提供
# 但是很多的廣告浴韭,特?zé)┤耍試L試腳本脯宿,最后因下載需求念颈,
# 加載了迅雷,這功能的實現(xiàn)小牛连霉,不過也是網(wǎng)絡(luò)別人共享的榴芳。。
from selenium import webdriver
import requests
from lxml import etree
import re
import os
from win32com.client import Dispatch
#test name of book : SciPy and NumPy
# book_name = input('Please input the book name in English:\n')
book_name = 'Introduction to Machine Learning with Python'
print ('begin to search book(s)...')
print ('---------------------------------')
# search link is :http://www.foxebook.nethttp://www.foxebook.net/search/SciPy%20and%20NumPySciPy%20and%20NumPy
PostUrl = "http://www.foxebook.net/search/" + book_name
# print(PostUrl)
# get the content of html
html = requests.get(PostUrl).content
# use etree selector
selector = etree.HTML(html)
# /html/body/div/div/main/div[2]/div[2]/h3/a
# /html/body/div/div/main/div[3]/div[2]/h3/a
# above is two books' xpath, so the right xpath for all book is :
# /html/body/div/div/main//div[2]/h3/a
# it can be confirmed by 'xpath checker'
total_books = selector.xpath("/html/body/div/div/main//div[2]/h3/a/text()")
# print('total books from searching are:', total_books)
num1 = 0
link_address = []
real_address = []
def find_link():
global num1
# find the right book, put all links in a list of : link_address
for i in total_books:
num1 += 1
if re.search(book_name,i):
print('Congrdulations, we find the book(s):\n')
print ('**********************************')
print(i)
print ('**********************************\n')
+ selector.xpath('//*[@id="content"]/div/main/div[%d]/div[2]/h3/a/@href'%num1)[0]
# print('the book link is :', href)
# print('will downloading...')
html_new = requests.get(href).content
selector_new = etree.HTML(html_new)
link_new = selector_new.xpath('//*[@id="download"]/div[2]/table/tbody/tr[1]/td[2]/a/@href')[0]
# split the next link
link_new = 'http:'+link_new.split(':')[-1]
link_address.append(link_new)
print('download link is :', link_address)
print('\n\n')
def real_book_link():
# print('link_address is :', link_address)
# dynamic on zippyshare
for j in link_address:
# 用瀏覽器實現(xiàn)訪問
driver = webdriver.Firefox()
driver.maximize_window()
driver.get(j)
try:
# find the download button
title_list = driver.find_element_by_xpath('//*[@id="dlbutton"]')
film_link = title_list.get_attribute('href')
real_address.append(film_link)
except:
print('can not download the book')
print('real_book_link:', real_address)
return real_address
def addTasktoXunlei(down_url,course_infos):
flag = False
o = Dispatch("ThunderAgent.Agent.1")
if down_url:
course_path = os.getcwd()
try:
#AddTask("下載地址", "另存文件名", "保存目錄","任務(wù)注釋","引用地址","開始模式", "只從原始地址下載","從原始地址下載線程數(shù)")
o.AddTask(down_url, '', course_path, "", "", -1, 0, 5)
o.CommitTasks()
flag = True
except Exception:
print(Exception.message)
print(" AddTask is fail!")
return flag
if __name__ == '__main__':
find_link()
real_link = real_book_link()
for i in real_link:
addTasktoXunlei(i, course_infos=None)
5窘面、第二天分析:
更換下載的書名為:《Introduction to Machine Learning with Python》
得到了2個有效的書籍目錄翠语,對比昨天的書籍名,發(fā)現(xiàn)提供的下載源是不同的國外網(wǎng)盤财边,而昨天的那個到今天一直打不開,而這本書的網(wǎng)址很快就打開了点骑,網(wǎng)盤名字為: zippyshare.com
然后研究了下酣难,此foxebook.net站點提供的一些網(wǎng)盤下載使用了多家國外網(wǎng)盤,并且各家的廣告顯示不盡相同黑滴,可靠性更是差別較大憨募。
另外,發(fā)現(xiàn)袁辈,就SciPy and NumPy一書來說菜谣,他最后得到的地址有2個http,這應(yīng)該是廣告模式晚缩,而后者的http的內(nèi)容是我們真實需要的尾膊,所以通過冒號:來切分a.split(':')[-1]。
In [10]: a = 'http://sh.st/st/7a45e8ed9f73a6a10e9a22b2d8783c44/http://www65.zippyshare.com/v/oFSWQWDk/file.html'
In [11]: a
Out[11]: 'http://sh.st/st/7a45e8ed9f73a6a10e9a22b2d8783c44/http://www65.zippyshare.com/v/oFSWQWDk/file.html'
In [12]: a.split(':')[-1]
Out[12]: '//www65.zippyshare.com/v/oFSWQWDk/file.html'
6荞彼、忘記說明下昨天的代碼為何要用re.match (或者re.research), 這是因為網(wǎng)站的關(guān)鍵詞搜索引擎所使用的算法冈敛,我們是不知道的,但從搜索結(jié)果看鸣皂,某關(guān)鍵詞下抓谴,可能有不同的書籍,而我們是需要精確搜索寞缝,下圖中實際出現(xiàn)了16本書癌压,但針對SciPy and NumPy,我們要找的是第三個圖對應(yīng)的荆陆。因此滩届,我們可以把顯示的書名做一個match對照的循環(huán),來實現(xiàn)精確匹配慎宾。而另外一方面丐吓,網(wǎng)站提供的書名還可能多了冒號浅悉,后面附加書名,這樣的也符合我們的要求券犁。后來發(fā)現(xiàn)用關(guān)鍵詞 if xxx in yyy的方式更簡便术健。
7、昨天的代碼一開始沒有考慮到國外網(wǎng)盤下載異常失敗的問題粘衬,并且有的搜索結(jié)果可能有多個網(wǎng)盤地址荞估,而我只取了默認(rèn)的第一個,考慮到下載的失敗可能性稚新,最好把所有下載地址都獲取勘伺。所以代碼需要修改。
由于:SciPy and NumPy 對應(yīng)的網(wǎng)盤當(dāng)機褂删,選用:《Introduction to Machine Learning with Python》為例
經(jīng)過對照飞醉,在最后的下載界面,是動態(tài)的屯阀,因為調(diào)用selenium+Firefox組合缅帘。最后終于得到了完整pdf隊中的鏈接,但速度明顯比較慢了难衰,在本例中钦无,是rar后綴的壓縮包格式,里面含有pdf盖袭。
download link is : ['http://www78.zippyshare.com/v/hBU7JYZp/file.html', 'http://www65.zippyshare.com/v/oFSWQWDk/file.html']
content:
book link: http://www78.zippyshare.com/d/hBU7JYZp/2248094/OReilly.Introduction.to.Machine.Learning.with.Python.A.Guide.for.Data.Scientists.1449369413.rar
content:
book link: http://www65.zippyshare.com/d/oFSWQWDk/1124867/OReilly.Introduction.to.Machine.Learning.with.Python.1449369413_Early.Release.rar
Process finished with exit code 0
8失暂、接下來的一個問題,怎么讓程序自動下載這2個鏈接鳄虱?群里有人推薦了一些別的軟件弟塞,但是我想來想去因為以后總要面對下載速度的問題,還是選定了迅雷破解版吧醇蝴,除非將來有其他更好的方案宣肚,好在有人共享了一個方案,還特別簡單悠栓,不過據(jù)說只能支持http格式霉涨,BT格式的以后再想辦法。
9:補充說明惭适,在正文代碼的第2個下載地址笙瑟,是有問題的,差別在于地址點擊后癞志,前者可在瀏覽器或者迅雷直接下載往枷,而后者瀏覽器沒反映,迅雷里下載的是一個html。盡管2個鏈接的提取方法完全一樣错洁,但一個好使秉宿,一個異常,由于是同一本書的前后2個小版本屯碴,我也不管他了描睦,但為了驗證迅雷是否能同時下載5個(代碼里設(shè)定同時下載的最大值,也是一般默認(rèn)值) 我用額外的測試腳本加載了一個新的鏈接导而,是證明可同時下載的忱叭,如圖:
9、參考:
10今艺、github對應(yīng)倉庫: