python3爬取torrent種子鏈接實例
本文環(huán)境是python3,采用的是urllib,BeautifulSoup搭建。
說下思路,這個項目分為管理器,url管理器,下載器,解析器,html文件生產(chǎn)器。各司其職,在管理器進行調(diào)度。最后將解析到的種子連接生產(chǎn)html文件顯示。當然也可以保存在文件。最后效果如圖。
首先在管理器SpiderMain()這個類的構(gòu)造方法里初始化下載器,解析器,html生產(chǎn)器。代碼如下。
def__init__(self): self.urls = url_manager.UrlManager() self.downloader = html_downloader.HtmlDownloader() self.parser = html_parser.HtmlParser() self.outputer = html_outputer.HtmlOutputer()
然后在主方法里寫入主連接并開始下載解析和輸出。
if __name__ == '__main__': url = "http://www.btany.com/search/桃谷繪里香-first-asc-1" # 解決中文搜索問題 對于:?=不進行轉(zhuǎn)義 root_url = quote(url,safe='/:?=') obj_spider = SpiderMain() obj_spider.parser(root_url)
用下載器進行下載,解析器解析下載好的網(wǎng)頁,最后輸出。管理器的框架邏輯就搭建完畢
def parser(self, root_url): html = self.downloader.download(root_url) datas = self.parser.parserTwo(html) self.outputer.output_html3(datas)
downloader下載器代碼如下:
def download(self, chaper_url):
if chaper_url is None:
return None
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=chaper_url, headers=headers)
response = urllib.request.urlopen(req)
if response.getcode() != 200:
return None
return response.read()
headers是模仿瀏覽器的請求頭。不然下載不到html文件。
解析器代碼如下:
# 解析種子文件
def parserTwo(self,html):
if html is None:
return
soup = BeautifulSoup(html,'html.parser',from_encoding='utf-8')
res_datas = self._get_data(soup)
return res_datas
# 將種子文件的標題,磁力鏈接和迅雷鏈接進行封裝
def _get_data(self,soup):
res_datas = []
all_data = soup.findAll('a',href=re.compile(r"/detail"))
all_data2 = soup.findAll('a', href=re.compile(r"magnet"))
all_data3 = soup.findAll('a',href=re.compile(r"thunder"))
for i in range(len(all_data)):
res_data = {}
res_data['title'] = all_data[i].get_text()
res_data['cl'] = all_data2[i].get('href')
res_data['xl'] = all_data3[i].get('href')
res_datas.append(res_data)
return res_datas
通過分析爬下來的html文件,種子鏈接在a標簽下。然后提取magnet和thunder下的鏈接。
最后輸出器輸出html文件,代碼如下:
def __init__(self):
self.datas = []
def collect_data(self, data):
if data is None:
return
self.datas.append(data)
#輸出表單
def output_html3(self,datas):
fout = open('output.html', 'w', encoding="utf-8")
fout.write("<html>")
fout.write("<head><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"></head>")
fout.write("<body>")
fout.write("<table border = 1>")
for data in datas:
fout.write("<tr>")
fout.write("<td>%s</td>" % data['title'])
fout.write("<td>%s</td>" % data['cl'])
fout.write("<td>%s</td>" % data['xl'])
fout.write("</tr>")
fout.write("</table>")
fout.write("</body>")
fout.write("</html>")
fout.close()

項目就結(jié)束了。源代碼已上傳,鏈接https://github.com/Ahuanghaifeng/python3-torrent,覺得有用請在github上給個star,您的鼓勵將是作者創(chuàng)作的動力。
以上這篇python3爬取torrent種子鏈接實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.setXmlVersion問題解決方法
- Python 識別12306圖片驗證碼物品的實現(xiàn)示例
- 使用 Python 處理3萬多條數(shù)據(jù)只要幾秒鐘
- python3連接mysql獲取ansible動態(tài)inventory腳本
- python3 Scrapy爬蟲框架ip代理配置的方法
- Python3 實現(xiàn)爬取網(wǎng)站下所有URL方式
- Ubuntu16.04安裝python3.6.5步驟詳解
- Python2和Python3中@abstractmethod使用方法
相關(guān)文章
python+requests接口自動化框架的實現(xiàn)
這篇文章主要介紹了python+requests接口自動化框架的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
使用Scrapy框架爬取網(wǎng)頁并保存到Mysql的實現(xiàn)
本文主要介紹了使用Scrapy框架爬取網(wǎng)頁并保存到Mysql的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
python opencv鼠標事件實現(xiàn)畫框圈定目標獲取坐標信息
這篇文章主要為大家詳細介紹了python opencv鼠標事件實現(xiàn)畫框圈定目標,獲取坐標信息,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
Python實現(xiàn)子類調(diào)用父類的初始化實例
這篇文章主要介紹了Python實現(xiàn)子類調(diào)用父類的初始化實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python 列表,數(shù)組,矩陣兩兩轉(zhuǎn)換tolist()的實例
下面小編就為大家分享一篇python 列表,數(shù)組,矩陣兩兩轉(zhuǎn)換tolist()的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python二進制文件轉(zhuǎn)換為文本文件的代碼實現(xiàn)
在日常編程中,我們經(jīng)常會遇到需要將二進制文件轉(zhuǎn)換為文本文件的情況,在Python中,我們可以利用各種庫和技術(shù)來完成這項任務,本文將介紹如何使用Python將二進制文件轉(zhuǎn)換為文本文件,并提供實用的代碼示例,需要的朋友可以參考下2024-04-04

