[Python] 纯文本查看 复制代码
import json
import time
import requests
from datetime import datetime
import mysql.connector
#请求头
headers = {
'authority': 'api.jijigugu.club',
'cache-control': 'max-age=0',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Microsoft Edge";v="96"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'
}
global lastScore, count, create_time, info_result,info_result_sql
lastScore = ""
info_result = ""
count = 1
create_time = int(time.time())
info_result_sql=[]
url = 'https://api.jijigugu.club/feed/list'
#获取json
def getInfo(Score):
params = (
('age', '["80+","80-","90+","00+","95+","05+","85+"]'),#这里可以自定义年龄段,比如只选择00+,删掉其他的就可以
('deviceId', ''),#替换你自己抓包的
('dist', 'appstore'),
('gender', '[2,1]'),#这里可以选择性别,[2,1]代表男女都选,[2]代表女生
('lastScore', Score),#lastScore是定位用的,每次请求都只有几个数据,lastScore可以让你获取更多
('mid', 'Db2moNRgpB2pQ4YrOW9w'),#Db2moNRgpB2pQ4YrOW9w代表的是求撩区,不同区有不同的mid,抓包即可获得
('model', 'iPhone14,5'),
('os_version', '15.0.2'),
('platform', '2'),
('secondsFromGMT', '28800'),
('smDeviceId', ''),#替换你自己抓包的
('version', '3.14.2'),
('x-jike-device-properties',
'{"idfv":"","idfa":""}'),#替换你自己抓包的
)
r = requests.get(url, headers=headers, params=params)
t = json.loads(r.text)
return t
#解析
def getList(t):
global count, create_time, info_result,info_result_sql
i = 0
for nickname in t['data']:
ttext = t['data'][i]['text']
text=ttext.replace("\n",",").replace(" ","")#这里是为了将动态格式化,替换换行为:,,替换空格,text是动态内容
age = t['data'][i]['age']#年龄段
gender = t['data'][i]['gender']#判断性别
if gender == 1:
sex = "男"
else:
sex = "女"
nickname = t['data'][i]['nickname']#发罐昵称
#获取发动态时间
createTime = t['data'][i]['createTime']
create_time = createTime
t1 = time.localtime(createTime)
ctime = time.strftime("%Y-%m-%d %H:%M:%S", t1)
likedNum = t['data'][i]['likedNum']#动态被喜欢数
commentedNum = t['data'][i]['commentedNum']]#动态被评论数
#获取图片
photo=t['data'][i]['photos']
purl = ""
if photo != []:
j=0
for url in photo:
turl=photo[j]['url']
purl += turl+';'
j+=1
#判断是否真身
isReal = t['data'][i]['user']['isReal']
if str(isReal) == "True":
iR = "真身"
else:
iR = "分身"
i += 1
#存入txt
info = str(count)+','+ str(nickname)+','+ str(iR)+','+str(age)+','+str(sex)+','+ str(ctime)+','+str(likedNum)+','+ str(commentedNum)+','+text+','+str(purl)+'\n'
info_result += info
#存入sql
try:
info_result_sql.append([count,nickname, iR, age, sex, ctime, likedNum, commentedNum, text, purl])
except:pass
count += 1
def main():
global lastScore
#1640923200为时间戳,获取到该时间之前的所有动态
while create_time > 1640923200:
t = getInfo(lastScore)
lastScore = t['lastScore']
getList(t)
#存入sql
table_name=mysql_Create()
mysql_Insert(table_name)
#存入txt
txt()
#存入txt
def txt():
with open("test.txt", "w") as f:
csv_title = "序号", "昵称", "真身", "年龄段", "性别", "创建时间", "喜欢数", "评论数", "内容", "图片地址"
f.write(str(csv_title) + '\n')
for row in info_result:
try:
f.write(str(row))
except:pass
f.close()
#创建sql表
def mysql_Create():
now = datetime.now()
table_name = "YG_" + now.strftime("%Y%m%d_%H%M")#动态现在时间创建表
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",#填你自己的
database=""#填你自己的
)
mycursor = mydb.cursor()
mycursor.execute("DROP TABLE IF EXISTS %s " % (table_name))
mycursor.execute("CREATE TABLE %s (count INT,nickname VARCHAR(20), iR VARCHAR(10), age VARCHAR(10), sex VARCHAR(5), ctime VARCHAR(20), likedNum INT, commentedNum INT, text TEXT, purl TEXT)" % (table_name))
mydb.commit()
mydb.close()
return table_name
#写入sql表
def mysql_Insert(table_name):
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",#填你自己的
database=""#填你自己的
)
mycursor = mydb.cursor()
for i in range(len(info_result_sql)):
try:
sql = "INSERT INTO %s (count,nickname,iR,age,sex,ctime,likedNum,commentedNum,text,purl) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"% (
table_name,info_result_sql[i][0], info_result_sql[i][1], info_result_sql[i][2], info_result_sql[i][3],
info_result_sql[i][4], info_result_sql[i][5], info_result_sql[i][6], info_result_sql[i][7],
info_result_sql[i][8], info_result_sql[i][9])
mycursor.execute(sql)
except:pass
mydb.commit()
mydb.close()
if __name__ == "__main__":
main()