鱼C论坛

 找回密码
 立即注册
查看: 3043|回复: 12

[已解决]爬虫挑战

[复制链接]
发表于 2023-11-28 01:56:38 | 显示全部楼层    本楼为最佳答案   
简单的看了一下,挺简单的,^_^
简单的说一下思路
先把主页面爬下来,看了看发现没有这个目录列表
看了下浏览器接收到的数据,发现了你提到的这个地址
  1. http://www.038909.xyz:5678/api/fs/list
复制代码


简单的研究了一下,得到了这个地址的使用方法
其实就是多看几个目录在这个地址上收到的数据,找规律么
然后就是根据找到的规律写代码了

另外,这个网站的反爬机制还不错
我目前最简单的解决方法也就是sleep一会了
1秒一个目录,出错了再10秒重试一下
如果这是你自己的网站,在测试爬虫的时候可以把这个目录弄的小一点么
我这爬了十几分钟了,才爬到 /体育/健身 这个目录,^_^
下面这个程序用于获取这个网站的整个目录树

  1. #!/usr/bin/env python
  2. #coding=utf-8

  3. import requests
  4. import json
  5. import itertools
  6. import time
  7. from retry import retry
  8. import sys
  9. import logging

  10. logging.basicConfig(stream = sys.stderr, level = logging.WARNING)
  11. headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}

  12. @retry(delay = 10, logger = logging.getLogger())
  13. def list_dir(path):
  14.     time.sleep(1)
  15.     print(path, file = sys.stderr)
  16.     content = []
  17.     for page in itertools.count(1):
  18.         json_ = {"path": path, "password": "", "page": page, "per_page": 30, "refresh": False}
  19.         url = 'http://www.038909.xyz:5678/api/fs/list'
  20.         response = requests.post(url, json = json_, headers = headers)
  21.         response.encoding = 'utf-8'
  22.         json_ = json.loads(response.text)
  23.         data = json_['data']
  24.         if data['content'] != None: content.extend(data['content'])
  25.         total = data['total']
  26.         if total == len(content): break
  27.     for i in content:
  28.         if not i['is_dir']: continue
  29.         i['dir_content'] = list_dir(path + i['name'] + '/')
  30.     return content

  31. content = list_dir('/')
  32. print(content)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-28 02:03:23 | 显示全部楼层
fineconey 发表于 2023-11-27 22:47
乱码了,路径如下。

还是直接用 api 吧,这样去拼 url 不得行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-11-28 19:58:25 | 显示全部楼层
再改一改,我发现文件没有爬到下载地址,需要使用下面这个地址再对文件进行一次请求
  1. http://www.038909.xyz:5678/api/fs/get
复制代码


爬了两个多小时才爬到 "/体育/健身/【赛普健身专业视频】价值19800元/实用工具图表/体质判断对照图",^_^
然后服务器断开连接了,看起来是我的ip被封了,^_^

  1. #!/usr/bin/env python
  2. #coding=utf-8

  3. import requests
  4. import json
  5. import itertools
  6. import time
  7. from retry import retry
  8. import sys
  9. import logging

  10. logging.basicConfig(stream = sys.stderr, level = logging.WARNING)
  11. headers = {"User-Agent": "Mozilla/6.0 (X11; Linux x86_64; rv:109.0) Gecko/20120101 Firefox/139.0"}
  12. dir_url = 'http://www.038909.xyz:5678/api/fs/list'
  13. file_url = 'http://www.038909.xyz:5678/api/fs/get'

  14. @retry(delay = 5, logger = logging.getLogger())
  15. def read_json(path, url, json_):
  16.     time.sleep(1)
  17.     response = requests.post(url, json = json_, headers = headers)
  18.     json_ = json.loads(response.text)
  19.     if json_['code'] != 200: raise ValueError(json_['message'])
  20.     return json_['data']

  21. def read_file(path):
  22.     print(path, file = sys.stderr)
  23.     json_ = {"path": path, "password": ""}
  24.     return read_json(path, file_url, json_)

  25. def read_dir(path):
  26.     print(path, file = sys.stderr)
  27.     content = []
  28.     for page in itertools.count(1):
  29.         json_ = {"path": path, "password": "", "page": page, "per_page": 30, "refresh": False}
  30.         data = read_json(path, dir_url, json_)
  31.         if data['content'] != None: content.extend(data['content'])
  32.         total = data['total']
  33.         if total == len(content): break
  34.     for i in range(len(content)):
  35.         is_dir = content[i]['is_dir']
  36.         name = content[i]['name']
  37.         if is_dir: content[i]['dir_content'] = read_dir(path + name + '/')
  38.         else: content[i] = read_file(path + name)
  39.     return content

  40. #content = read_dir('/游戏/PC/07.运行库/')
  41. content = read_dir('/')
  42. print(content)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-28 21:03:18 | 显示全部楼层
fineconey 发表于 2023-11-28 20:20
结构是一样的,可以试试这个。

https://pan.mediy.cn/

需要我继续挂这个脚本去爬整个目录树吗?
^_^
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-3 08:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表