温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python3如何爬取torrent种子链接

发布时间:2021-06-28 15:02:50 来源:亿速云 阅读:362 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关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()

python3如何爬取torrent种子链接

感谢各位的阅读!关于“python3如何爬取torrent种子链接”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI