|
| 1 | +import requests |
| 2 | + |
| 3 | +headers = { |
| 4 | + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36", |
| 5 | + "Origin": "https://bbs.mihoyo.com", |
| 6 | + "Referer": "https://bbs.mihoyo.com/", |
| 7 | + "Host": "bbs-api.mihoyo.com" |
| 8 | + } |
| 9 | + |
| 10 | +def request_get(url, ret_type): |
| 11 | + |
| 12 | + res = requests.get(url=url, headers=headers, timeout= 5) |
| 13 | + res.encoding = "utf-8" |
| 14 | + if ret_type == "text": |
| 15 | + return res.text |
| 16 | + elif ret_type == "image": |
| 17 | + return res.content |
| 18 | + elif ret_type == "json": |
| 19 | + return res.json() |
| 20 | + |
| 21 | +def main(last_id): |
| 22 | + url = f"https://bbs-api.mihoyo.com/post/wapi/getForumPostList??game_id=5&gids=5&last_id={last_id}&list_type=0&page_size=20&topic_id=547" |
| 23 | + res_json = request_get(url, "json") |
| 24 | + if res_json["retcode"] == 0: |
| 25 | + for item in res_json["data"]["list"]: |
| 26 | + post_id = item["post"]["post_id"] |
| 27 | + detail(post_id) |
| 28 | + |
| 29 | +def detail(post_id): |
| 30 | + url = f"https://bbs-api.mihoyo.com/post/wapi/getPostFull?gids=5&post_id={post_id}&read=1" |
| 31 | + res_json = request_get(url, "json") |
| 32 | + if res_json["retcode"] == 0: |
| 33 | + image_list = res_json["data"]["post"]["image_list"] |
| 34 | + for img in image_list: |
| 35 | + img_url = img["url"] |
| 36 | + if (img_url.find("weigui")) < 0: |
| 37 | + save_image(img_url) |
| 38 | + |
| 39 | + |
| 40 | +def save_image(image_src): |
| 41 | + r = requests.get(image_src) |
| 42 | + content = r.content |
| 43 | + name = image_src.split('/')[-1] |
| 44 | + with open('D://mhy//' + name, "wb") as f: |
| 45 | + f.write(content) |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + main(18136074) |
| 53 | + |
| 54 | +def main(last_id): |
| 55 | + url = f"https://bbs-api.mihoyo.com/post/wapi/getForumPostList?forum_id=47&gids=5&is_good=false&last_id={last_id}&is_hot=false&page_size=20&sort_type=2" |
| 56 | + res_json = request_get(url, "json") |
| 57 | + if res_json["retcode"] == 0: |
| 58 | + for item in res_json["data"]["list"]: |
| 59 | + detail(item["post"]["post_id"]) |
| 60 | + |
| 61 | + if res_json["data"]["last_id"] != "": |
| 62 | + return main(res_json["data"]["last_id"]) |
| 63 | + |
| 64 | +if __name__ == '__main__': |
| 65 | + main(18136074) |
0 commit comments