Skip to content

Commit 401c1c2

Browse files
committed
ADD:增加微博爬虫
1 parent 58458ac commit 401c1c2

File tree

22 files changed

+647
-5
lines changed

22 files changed

+647
-5
lines changed

data/weibo/.gitkeep

Whitespace-only changes.

docs/api/weibo/weibo.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# API 文档
2+
3+
## 微博
4+
5+
### 添加账号
6+
7+
- **功能说明**
8+
9+
用于添加微博网页版账号,cookie从`https://weibo.com/`的请求中获取。
10+
11+
- **URL**
12+
13+
`/weibo/add_account`
14+
15+
- **Method**
16+
17+
`POST`
18+
19+
- **Data Params**
20+
21+
| 参数 | 必选 | 类型 | 说明 |
22+
|:---:|:---:|:---:|:---:|
23+
| id | true | string | 账户名(用于管理用户cookie) |
24+
| cookie | true | string | 微博cookie |
25+
26+
- **Response**
27+
28+
| 参数 | 必选 | 类型 | 说明 |
29+
|:---:|:---:|:---:|:---:|
30+
| code | true | int | 0: 成功 1: 参数错误 2: 服务器错误 |
31+
| data | true | struct | 数据 |
32+
| msg | true | string | 请求说明(成功、参数错误、服务器错误) |
33+
34+
### 获取账号列表
35+
36+
- **URL**
37+
38+
`/weibo/account_list`
39+
40+
- **Method**
41+
42+
`GET`
43+
44+
- **URL Params**
45+
46+
None
47+
48+
- **Response**
49+
50+
| 参数 | 必选 | 类型 | 说明 |
51+
|:---:|:---:|:---:|:---:|
52+
| code | true | int | 0: 成功 1: 参数错误 2: 服务器错误 |
53+
| data | true | list | [ [账户信息](#账户信息) ] |
54+
| msg | true | string | 请求说明(成功、参数错误、服务器错误) |
55+
56+
#### 账户信息
57+
58+
| 参数 | 必选 | 类型 | 说明 |
59+
|:---:|:---:|:---:|:---:|
60+
| id | true | string | 账户名(用于管理用户cookie) |
61+
| cookie | true | string | 微博cookie |
62+
| ct | true | int | 创建时间戳 |
63+
| ut | true | int | 更新时间戳 |
64+
| expired | true | int | 0: 有效 1: 过期 (请求失败时自动设为过期) |
65+
66+
### 获取微博详情
67+
68+
- **功能说明**
69+
70+
由于微博允许游客访问,因此此接口可以不需要添加账号,直接调用。
71+
72+
- **URL**
73+
74+
`/weibo/detail`
75+
76+
- **Method**
77+
78+
`GET`
79+
80+
- **URL Params**
81+
82+
| 参数 | 必选 | 类型 | 说明 |
83+
|:---:|:---:|:---:|:---:|
84+
| id | true | string | 微博id,从`https://m.weibo.cn/`找到需要的帖子打开详情页,url中`detail/`后面的数字就是id |
85+
86+
- **Success Response**
87+
88+
| 参数 | 必选 | 类型 | 说明 |
89+
|:---:|:---:|:---:|:---:|
90+
| code | true | int | 0: 成功 1: 参数错误 2: 服务器错误 |
91+
| data | true | struct | 数据 |
92+
| msg | true | string | 请求说明(成功、参数错误、服务器错误) |
93+
94+
### 获取微博评论
95+
96+
- **URL**
97+
98+
`/weibo/comments`
99+
100+
- **Method**
101+
102+
`GET`
103+
104+
- **URL Params**
105+
106+
| 参数 | 必选 | 类型 | 说明 |
107+
|:---:|:---:|:---:|:---:|
108+
| id | true | string | 微博id,从`https://m.weibo.cn/`找到需要的帖子打开详情页,url中`detail/`后面的数字就是id |
109+
| offset | false | int | 评论翻页偏移量, 默认0 |
110+
| limit | false | int | 评论数量, 默认20 |
111+
112+
- **Success Response**
113+
114+
| 参数 | 必选 | 类型 | 说明 |
115+
|:---:|:---:|:---:|:---:|
116+
| code | true | int | 0: 成功 1: 参数错误 2: 服务器错误 |
117+
| data | true | struct | 数据 |
118+
| msg | true | string | 请求说明(成功、参数错误、服务器错误) |
119+
120+
### 获取评论回复
121+
122+
- **URL**
123+
124+
`/weibo/replys`
125+
126+
- **Method**
127+
128+
`GET`
129+
130+
- **URL Params**
131+
132+
| 参数 | 必选 | 类型 | 说明 |
133+
|:---:|:---:|:---:|:---:|
134+
| id | true | string | 微博id,从`https://m.weibo.cn/`找到需要的帖子打开详情页,url中`detail/`后面的数字就是id |
135+
| comment_id | true | string | 微博评论id,从评论中获得到的id,例如: 5045096886306039 |
136+
| offset | false | int | 评论翻页偏移量, 默认0 |
137+
| limit | false | int | 评论数量, 默认20 |
138+
139+
- **Success Response**
140+
141+
| 参数 | 必选 | 类型 | 说明 |
142+
|:---:|:---:|:---:|:---:|
143+
| code | true | int | 0: 成功 1: 参数错误 2: 服务器错误 |
144+
| data | true | struct | 数据 |
145+
| msg | true | string | 请求说明(成功、参数错误、服务器错误) |
146+
147+
### 关键词搜索微博
148+
149+
- **URL**
150+
151+
`/weibo/search`
152+
153+
- **Method**
154+
155+
`GET`
156+
157+
- **URL Params**
158+
159+
| 参数 | 必选 | 类型 | 说明 |
160+
|:---:|:---:|:---:|:---:|
161+
| keyword | true | string | 搜索词 |
162+
| offset | false | int | 搜索翻页偏移量, 默认0 |
163+
| limit | false | int | 结果数量, 默认10 |
164+
165+
- **Success Response**
166+
167+
| 参数 | 必选 | 类型 | 说明 |
168+
|:---:|:---:|:---:|:---:|
169+
| code | true | int | 0: 成功 1: 参数错误 2: 服务器错误 |
170+
| data | true | struct | 数据 |
171+
| msg | true | string | 请求说明(成功、参数错误、服务器错误) |
172+
173+
### 微博媒体预览
174+
175+
- **功能说明**
176+
177+
由于微博所有接口返回的媒体资源链接均验证了`referer`,因此不能直接在网页中预览,此接口提供了预览功能,用法为`http://127.0.0.1:8080/weibo/preview?url=xxx`,其中`xxx`替换为媒体资源的地址(请求返回包含参数)即可。
178+
179+
- **URL**
180+
181+
`/weibo/preview`
182+
183+
- **Method**
184+
185+
`GET`
186+
187+
- **URL Params**
188+
189+
| 参数 | 必选 | 类型 | 说明 |
190+
|:---:|:---:|:---:|:---:|
191+
| url | true | string | 媒体地址 |
192+
193+
- **Success Response**
194+
195+
| 参数 | 必选 | 类型 | 说明 |
196+
|:---:|:---:|:---:|:---:|
197+
| code | true | int | 0: 成功 1: 参数错误 2: 服务器错误 |
198+
| data | true | struct | 数据 |
199+
| msg | true | string | 请求说明(成功、参数错误、服务器错误) |

docs/doc.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,17 @@
7171
- 京东搜索结果获取
7272

7373
京东:[API 文档](api/jd/jd.md)
74+
75+
## 微博
76+
77+
目前支持以下接口:
78+
79+
- 添加微博账号
80+
- 获取微博账号列表
81+
- 微博视频信息获取
82+
- 微博视频评论获取
83+
- 微博评论回复获取
84+
- 微博搜索结果获取
85+
- 微博媒体资源预览
86+
87+
微博:[API 文档](api/weibo/weibo.md)

docs/record.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
- MOD: 修改所有回复接口视频/笔记id参数名为id
1818
- FIX: 修复哔哩哔哩视频html匹配问题导致的无法爬取
1919
- FIX: 修复sqlite数据库多线程连接池问题
20+
21+
## 2024-06-14
22+
23+
- ADD: 支持微博常见接口

main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from service.xhs.urls import xhs
66
from service.taobao.urls import taobao
77
from service.jd.urls import jd
8+
from service.weibo.urls import weibo
89
from lib.logger import logger
910
import yaml
1011

@@ -15,6 +16,7 @@
1516
app.register_blueprint(xhs, url_prefix="/xhs")
1617
app.register_blueprint(taobao, url_prefix="/taobao")
1718
app.register_blueprint(jd, url_prefix="/jd")
19+
app.register_blueprint(weibo, url_prefix="/weibo")
1820

1921
def init_service():
2022
config_file = "config/config.yaml"

readme.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# 服务 - 主流媒体平台爬虫
22

3-
抖音、快手、哔哩哔哩、小红书、淘宝、京东平台爬虫服务器项目。它可以获取这些平台上的公开信息,但请遵守下方的的免责声明。
3+
抖音、快手、哔哩哔哩、小红书、淘宝、京东、微博平台爬虫服务器项目。它可以获取这些平台上的公开信息,但请遵守下方的的免责声明。
44

55
## 免责声明
66

77
本项目的初衷是为了帮助开发者更好地了解和掌握这些平台,也希望能帮助企业和安全工程师了解爬虫手段,提高平台的反爬虫措施,而不是用于非法用途,反对用于任何违反相关法律法规的行为。
88

99
仅供学习和研究使用,不得用于任何商业用途或非法目的。使用本项目提供的功能时,用户需自行承担可能带来的一切法律责任。
1010

11-
我们提供的爬虫仅能获取抖音,快手、哔哩哔哩、小红书、淘宝、京东等平台上公开的信息,我们强烈反对任何形式的隐私侵犯行为。如果你使用本项目进行了侵犯他人隐私的行为,我们将与你保持距离,并支持受害者通过法律手段维护自己的权益。
11+
我们提供的爬虫仅能获取抖音,快手、哔哩哔哩、小红书、淘宝、京东、微博平台上公开的信息,我们强烈反对任何形式的隐私侵犯行为。如果你使用本项目进行了侵犯他人隐私的行为,我们将与你保持距离,并支持受害者通过法律手段维护自己的权益。
1212

1313
使用本仓库的内容即表示您同意本免责声明的所有条款和条件。如果你不接受以上的免责声明,请立即停止使用本项目。
1414

@@ -18,10 +18,11 @@
1818

1919
## 功能
2020

21-
- 快手、抖音、哔哩哔哩、小红书、淘宝、京东平台的爬虫接口
21+
- 快手、抖音、哔哩哔哩、小红书、淘宝、京东、微博平台的爬虫接口
2222
- 获取公开的用户信息,帖子信息等
2323
- 使用简单,支持多种语言使用HTTP调用
2424
- 哔哩哔哩视频[一键下载](docs/api/bilibili/bilibili.md#bilibili视频下载)
25+
- 微博媒体资源[预览接口](docs/api/weibo/weibo.md#微博媒体预览)
2526

2627
## 快速开始
2728

@@ -73,7 +74,7 @@
7374
cat .log/crawler.log # access.log error.log
7475
```
7576

76-
首次使用需添加账号,例如:调用`http://localhost:8080/{platfrom}/add_account`添加账号,platfrom取`douyin``kuaishou``bilibili``xhs``taobao``jd`,接口详情请参考[API 文档](docs/doc.md)。
77+
首次使用需添加账号,例如:调用`http://localhost:8080/{platfrom}/add_account`添加账号,platfrom取`douyin``kuaishou``bilibili``xhs``taobao``jd``weibo`,接口详情请参考[API 文档](docs/doc.md)。
7778

7879
然后在你的代码中调用API,获取公开的信息。
7980

@@ -91,7 +92,9 @@ API文档:[API 文档](docs/doc.md)
9192

9293
- [x] Bilibili
9394
- [x] 小红书
94-
- [ ] 微博
95+
- [x] 微博
96+
- [ ] 淘宝详情、评论功能
97+
- [ ] 京东详情、评论功能
9598

9699
## 更新记录
97100

service/weibo/logic/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .detail import request_detail
2+
from .comments import request_comments
3+
from .replys import request_replys
4+
from .search import request_search

service/weibo/logic/comments.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from .common import common_request
2+
3+
def request_comments(id: str, cookie: str, offset: int = 0, limit: int = 20) -> tuple[dict, bool]:
4+
"""
5+
请求微博获取评论信息
6+
"""
7+
headers = {"cookie": cookie}
8+
end_length = offset + limit
9+
comments = []
10+
max_id = 0
11+
total = 0
12+
is_end = False
13+
while not is_end and len(comments) < end_length:
14+
params = {
15+
"id": id,
16+
"is_show_bulletin": 2,
17+
"is_mix": 0,
18+
"count": 10,
19+
# "uid": 123, # 可通过profile获取,非强制参数
20+
"fetch_level": 0,
21+
"locale": "zh-CN",
22+
"max_id": max_id
23+
}
24+
resp, succ = common_request('/ajax/statuses/buildComments', params, headers)
25+
if not succ:
26+
return resp, succ
27+
comments.extend(resp.get('data', []))
28+
max_id = int(resp.get('max_id', 0))
29+
total = resp.get('total_number', 0)
30+
is_end = max_id == 0
31+
32+
ret = {'total': total, 'comments': comments[offset:end_length]}
33+
return ret, succ

0 commit comments

Comments
 (0)