對於喜歡電影的人來說各種電影資源必不可少,但每次自己搜索都比較麻煩,索性用python自己寫一個自動搜索的腳本。
這里我只分享我的思路,具體如何實現參考代碼,要想實現搜索功能先要抓包分析如何發送數據,這里我用的是burp,
這是電影網站搜索框,
輸入電影名抓取數據報:
數據一get方式提交,並且進行了url編碼,%E9%BB%91%E8%B1%B9進行url解碼后正是“黑豹”兩個字
python中用於處理url編碼的是urllib中的quote模塊
name=黑豹
uname=quote(name)
所以我們提交數據的地址為:url='http://www.btbtdy.com/search/'+uname+'.html'
之后就得到這個界面:

我們只需要拿到最頂端的那個連接就行,直接用beautifulsoup進行匹配也可以用re正則匹配,找到“黑豹"兩個字的herf屬性即可
最后得到的數據為”/btdy/dy7706.html",與原網址進行拼接記得到我們要找電影資源的主頁面為:
http://www.btbtdy.com/btdy/dy7706.html
到達主頁面后,如果你直接用以前的辦法直接用正則或其他的辦法去匹配磁力鏈接的話是不行的,因為這是一個動態的頁面,
思路依舊是抓包分析,可以看出主頁面提交后有提交多個其他的請求,其中有也個請求是這樣的:

在網頁上訪問后是這樣的:

這才是我們要找的網頁,只有在這個網頁上才能找到真正的資源
上代碼:(代碼還沒有進行異常處理)
1 import requests 2 from bs4 import BeautifulSoup 3 4 from urllib.parse import quote 5 import time 6 import re 7 import threading 8 9 head = { 10 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', 11 'Referer':'http://www.btbtdy.com/' 12 } 13 14 print('-----------------------------') 15 name=input('請輸入需要查找的電影:') 16 print('-----------------------------') 17 uname=quote(name) 18 19 def pyhead(): 20 21 url='http://www.btbtdy.com/search/'+uname+'.html' 22 23 return url 24 25 def gethtml(url): 26 27 link=url 28 html=requests.get(link,head) 29 time.sleep(5) 30 soup = BeautifulSoup(html.text, "lxml") 31 html = html.content.decode('utf-8') 32 sorry="對不起,沒有找到任何記錄," 33 sodiv=soup.find('div',class_="list_so") 34 if sorry in str(sodiv): 35 print("網站沒有資源") 36 else: 37 title=soup.find_all('a',class_="so_pic") 38 r=r'href="(.+?)" ' 39 title=re.findall(r,str(title[0])) 40 print("網址為:http://www.btbtdy.com"+title[0]) 41 return title 42 43 def gethtml2(title): 44 dr=r'btdy/dy(.+?).html' 45 dtit=re.findall(dr,title[0]) 46 url2='http://www.btbtdy.com/vidlist/'+dtit[0]+'.html' 47 dhtml=requests.get(url2,head) 48 time.sleep(5) 49 dsoup=BeautifulSoup(dhtml.text,'lxml') 50 return dsoup 51 52 def getdhtml(dsoup): 53 ddiv=dsoup.find_all('div',class_="p_list") 54 for model in ddiv: 55 h="<h2>720p下載地址</h2>" 56 h2="<h2>1080p下載地址</h2>" 57 h3="<h2>下載地址一</h2>" 58 if h in str(model): 59 print("720p:"'\n') 60 r='<a class="d1" href="(.+?)">磁力</a>' 61 dlink=re.findall(r,str(model)) 62 for pdlink in dlink: 63 print(str(pdlink)+'\n') 64 if h2 in str(model): 65 print("1080p:"'\n') 66 r='<a class="d1" href="(.+?)">磁力</a>' 67 dlink=re.findall(r,str(model)) 68 for pdlink in dlink: 69 print(str(pdlink)) 70 if h3 in str(model): 71 print("磁力連接:"'\n') 72 r='<a class="d1" href="(.+?)">磁力</a>' 73 dlink=re.findall(r,str(model)) 74 for pdlink in dlink: 75 print(str(pdlink)+'\n') 76 77 78 def start(): 79 url=pyhead() 80 title=gethtml(url) 81 dsoup=gethtml2(title) 82 getdhtml(dsoup) 83 if __name__ == '__main__': 84 go=threading.Thread(start()) 85 go.start()