-
-
Save bestK/dfe5e8a5e158f87d1be721257add5b1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
from bs4 import BeautifulSoup | |
from fake_useragent import UserAgent | |
# 创建一个UserAgent对象来生成随机的User-Agent头 | |
ua = UserAgent() | |
# 发送HTTP请求并获取页面内容 | |
url = "https://invitecode.cc/" | |
headers = {"User-Agent": ua.random} | |
response = requests.get(url, headers=headers) | |
# 检查响应状态码 | |
if response.status_code == 200: | |
# 使用BeautifulSoup解析页面内容 | |
soup = BeautifulSoup(response.text, "html.parser") | |
# 找到id为'app'的div元素 | |
app_div = soup.find("div", {"id": "app"}) | |
# 获取div的data-page属性 | |
data_page = app_div.get("data-page") | |
# 将data-page属性值转换为JSON对象 | |
json_data = json.loads(data_page) | |
posts = json_data["props"]["posts"]["data"] | |
for p in posts: | |
if p["available_count"] > 0: | |
print(p["title"], p["available_count"]) | |
hash_id = p["hash_id"] | |
post_url = "https://invitecode.cc/post/" + hash_id | |
headers = {"User-Agent": ua.random} | |
response = requests.get(post_url, headers=headers) | |
soup = BeautifulSoup(response.text, "html.parser") | |
# 找到id为'app'的div元素 | |
app_div = soup.find("div", {"id": "app"}) | |
# 获取div的data-page属性 | |
data_page = app_div.get("data-page") | |
# 将data-page属性值转换为JSON对象 | |
json_data = json.loads(data_page) | |
codes = json_data["props"]["codes"] | |
for c in codes: | |
if c["is_public"] == False: | |
print(f'id:{c["id"]} code: {c["content"]}') | |
# TODO 伪造 anonymous_id | |
else: | |
print("Failed to retrieve the page. Status code: ", response.status_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment