Initial commit.
This commit is contained in:
commit
e2f1a31d0e
3 changed files with 58 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
__pycache__/
|
||||
*.pyc
|
||||
*.swp
|
||||
build/
|
7
README.md
Normal file
7
README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
# weibo-lottery
|
||||
|
||||
一个简单的脚本,用来把一条微博的所有转发保存到本地的 CSV 文件。
|
||||
|
||||
## License
|
||||
|
||||
[The MIT License](https://sunoru.mit-license.org/)
|
47
main.py
Normal file
47
main.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
import csv
|
||||
import time
|
||||
import requests
|
||||
|
||||
import fire
|
||||
|
||||
RepostAPI = "https://m.weibo.cn/api/statuses/repostTimeline"
|
||||
|
||||
class Main():
|
||||
def _get_repost_page(self, weiboId, page, session=requests):
|
||||
params = {"id": weiboId, "page": page}
|
||||
response = session.get(RepostAPI, params=params)
|
||||
return response.json()
|
||||
|
||||
def _get_reposts(self, cookie, weiboId, wait):
|
||||
max_page = None
|
||||
i = 0
|
||||
with requests.Session() as session:
|
||||
session.headers.update({"Cookie": cookie})
|
||||
while max_page is None or i < max_page:
|
||||
i += 1
|
||||
print(f"Fetching page {i}...")
|
||||
page = self._get_repost_page(weiboId, i, session)
|
||||
if max_page is None:
|
||||
max_page = page["data"]["max"]
|
||||
for repost in page["data"]["data"]:
|
||||
yield repost
|
||||
time.sleep(wait)
|
||||
|
||||
def _to_row(self, repost):
|
||||
user = repost["user"]["screen_name"]
|
||||
time = repost["created_at"]
|
||||
text = repost["raw_text"]
|
||||
url = f'https://www.weibo.com/{repost["user"]["id"]}/{repost["bid"]}'
|
||||
return [user, time, text, url]
|
||||
|
||||
def fetch_reposts(self, output, wait=1):
|
||||
weiboId = input("Weibo ID: ")
|
||||
cookie = input("Cookie: ")
|
||||
with open(output, "w", newline="") as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerow(["User", "Time", "Text", "URL"])
|
||||
for repost in self._get_reposts(cookie, weiboId, wait):
|
||||
writer.writerow(self._to_row(repost))
|
||||
print(f"Reposts saved to {output}")
|
||||
|
||||
fire.Fire(Main)
|
Loading…
Reference in a new issue