python3爬取torrent种子链接实例
作者:海峰-清欢 发布时间:2022-03-05 11:23:44
标签:python3,torrent,种子,链接
本文环境是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
0
投稿
猜你喜欢
- 常见的误解有: 1. 只用 ado.net ,无法进行动态 SQL 拼接。 2. 有几个动态参数,代码的重复量就成了这些参数的不同数量的组合
- 目录1. 常用的编码2.补充:计算机表示的单位:3.ASCII编码2.GBK和GB2312编码4.Unicode5.UTF-8编码6.编码和
- 在 .NET Framework 2.0中,ConfigurationManager类新增了两个属性AppSe
- 如何在网站上提供音乐下载?为用户提供歌曲下载,一般有两种方式,一是直接通过Http,浏览器下载,二是通过ftp协议下载。我们来用Http和浏
- 环境系统:win10cpu:i7-6700HQgpu:gtx965mpython : 3.6pytorch :0.3数据下载来源自Sasan
- 一、前言 英语单词之间是通过空格分隔的,但是中文却不存在空格的概念,因此需要
- Python for 循环语句Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。语法:for循环的语法格
- 以下是涉及到插入表格的查询的5种改进方法:1)使用LOAD DATA INFILE从文本下载数据这将比使用插入语句快20倍。2)使用带有多个
- MySQL 临时表在我们需要保存一些临时数据时是非常有用的。临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间。临时
- MySQL性能优化在互联网公司MySQL的使用非常广泛,大家经常会有MySQL性能优化方面的需求。整理了一些在MySQL优化方面的实用技巧。
- Python 使用 selenium 进行自动化测试 或者协助日常工作,内容如下所示:1、基础准备需要准备 Python 环境需要安装 se
- 实例如下所示:u = array([[1,2],[3,4]])m = u.tolist()#转换为listm.remove(m[0])#移除
- use 数据库 go EXEC sp_changeobjectowner ‘原表的所有者.表名',现在的所有者例如: exec sp
- 在document.form1.submit();后加document.body.innerHtml = "W
- 数组元素添加、删除和修改数组也是一个可变类型,可以对数组中的元素进行添加、删除和修改,本文详细介绍了对数组元素的添加和删除的操作,以及这两种
- 在业务稳定性要求比较高的情况下,运维为能及时发现问题,有时需要对应用程序的日志进行实时分析,当符合某个条件时就立刻报警,而不是被动等待出问题
- 1.复制表结构及数据到新表CREATE TABLE 新表SELECT * FROM 旧表 2.只复制表结构到新表CREATE TABLE 新
- mysql的存储过程、游标 、事务实例详解下面是自己曾经编写过的mysql数据库存储过程,留作存档,以后用到的时候拿来参考。其中,涉及到了存
- 1、弹出菜单也叫上下文菜单,建立菜单并向菜单添加各种功能。2、右键监听鼠标。如右键点击,则根据位置判断弹出。3、调用Menupop方法。4、
- 一、为 SQL 启用远程连接 1. 单击“开始”,依次指向“程序”、“Microsoft SQL Server 2005”和“配置工具”,然