python3爬取torrent种子链接实例
作者:海峰-清欢 时间:2022-03-05 11:23:44
本文环境是python3,采用的是urllib,BeautifulSoup搭建。
说下思路,这个项目分为管理器,url管理器,下载器,解析器,html文件生产器。各司其职,在管理器进行调度。最后将解析到的种子连接生产html文件显示。当然也可以保存在文件。最后效果如图。
首先在管理器SpiderMain()这个类的构造方法里初始化下载器,解析器,html生产器。代码如下。
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"
# 解决中文搜索问题 对于:?=不进行转义
root_url = quote(url,safe='/:?=')
obj_spider = SpiderMain()
obj_spider.parser(root_url)
用下载器进行下载,解析器解析下载好的网页,最后输出。管理器的框架逻辑就搭建完毕
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()
项目就结束了。源代码已上传,链接https://github.com/Ahuanghaifeng/python3-torrent,觉得有用请在github上给个star,您的鼓励将是作者创作的动力。
来源:https://blog.csdn.net/u013692888/article/details/52660492
标签:python3,torrent,种子,链接
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解如何使用Python实现删除重复文件
2022-08-14 04:33:54
![](https://img.aspxhome.com/file/2023/5/67895_0s.png)
利用python写个下载teahour音频的小脚本
2021-05-17 06:05:54
Python实现获取邮箱内容并解析的方法示例
2021-07-31 01:40:11
php读取mysql的简单实例
2023-11-15 08:57:51
PHP利用ChatGPT实现轻松创建用户注册页面
2023-05-25 09:22:16
写出完美CSS代码的5个重要方面
2009-12-30 16:44:00
Django框架模型简单介绍与使用分析
2021-04-06 02:59:19
![](https://img.aspxhome.com/file/2023/6/92506_0s.png)
Python MySQLdb 使用utf-8 编码插入中文数据问题
2023-07-31 11:04:13
python中的json数据和pyecharts模块入门示例教程
2023-02-22 04:58:37
![](https://img.aspxhome.com/file/2023/9/87659_0s.jpg)
Python调用C# Com dll组件实战教程
2023-09-05 10:46:06
![](https://img.aspxhome.com/file/2023/8/71748_0s.png)
python 实现循环定义、赋值多个变量的操作
2023-10-24 08:44:20
python中使用PIL制作并验证图片验证码
2023-06-05 11:28:33
一段Asp301重定向过程代码
2010-05-04 16:38:00
PHP实现的获取文件mimes类型工具类示例
2023-10-07 09:33:33
Keras loss函数剖析
2021-01-24 05:25:27
python函数形参用法实例分析
2023-09-08 21:07:09
一段重用很高的ajax代码
2009-02-09 13:27:00
php进程daemon化的正确实现方法
2023-10-01 09:14:09
pytorch使用-tensor的基本操作解读
2022-05-23 03:15:12
![](https://img.aspxhome.com/file/2023/3/95443_0s.png)
Python3爬虫之urllib携带cookie爬取网页的方法
2022-07-01 14:19:02