本帖最后由 w1748 于 2023-1-30 00:15 编辑
最新代码:https://github.com/UnAbuse/reptile.inclued,过年没时间在这上面更新,后面有时间一起改了。
平时无事喜欢看小说,可是某平台的广告让人非常苦恼。
于是我花了点时间找了下api,对api进行数据处理,然后发现返回为空。。。。原来是要登录,相信大部分人对于cookie的获取已经轻车熟路,所以我在这里只放源码供大家娱乐。
import Gh
class FanQie:
def __init__(self):
# 引入二次封装的网络请求函数
self.Meth = Gh.Meth()
# 定义基本参数
Cookie = ''
self.search_url = 'https://novel.snssdk.com/api/novel/channel/homepage/search/search/v1/'
self.headers = {'User-Agent': 'Mozilla/5.0 (Danger hiptop 3.4; U; AvantGo 3.2)', 'Cookie': Cookie}
self.encoding = 'utf-8'
self.directory_url = 'https://api5-normal-lf.fqnovel.com/reading/bookapi/directory/all_items/v/'
self.content_url = 'https://novel.snssdk.com/api/novel/book/reader/full/v1/'
def search(self, keywords):
# 获得书本基本信息
params = {'aid': 13,'q': keywords}
book_list_info = self.Meth.get_Html(self.search_url, 'GET', 'json', self.headers, self.encoding, params)
book_list = book_list_info['data']['ret_data']
book_list_clear = []
for items in book_list: book_list_clear.append({'title': items['title'], 'author': items['author'], 'category': items['category'], 'book_id': items['book_id'], 'abstract': items['abstract']})
return book_list_clear
def direct(self, book_id):
# 获取书本目录
params = {'book_id': book_id, 'aid': 1967, 'iid': 2665637677906061, 'app_name': 'novelapp', 'version_code': 495}
direc_list_info = self.Meth.get_Html(self.directory_url, 'GET', 'json', self.headers, self.encoding, params)
direc_list = direc_list_info['data']['item_data_list']
direc_list_clear = []
for items in direc_list: direc_list_clear.append({'title': items['title'], 'volume_name': items['volume_name'], 'item_id': items['item_id'],'content_md5': items['content_md5']})
return direc_list_clear
def content(self, item_id):
# 获取当前章节正文内容
params = {'group_id': item_id, 'item_id': item_id}
content_info = self.Meth.get_Html(self.content_url, 'GET', 'json', self.headers, self.encoding, params)
try: return content_info['data']['content']
except: return '未配置cookie或者cookie失效'
import requests
from random import randint
class Meth:
def __init__(self, proxy_list=None):
# 定义代{过}{滤}理ip的列表
self.proxy_list = None
if self.proxy_list is None: self.proxy_list = []
else: self.proxy_list = proxy_list
def get_ip(self, filename):
# 从文件中读取代{过}{滤}理ip
with open(filename, 'r+', encoding='utf-8') as file_data:
data = file_data.readlines()
for items in data:
items = items.strip('\n')
self.proxy_list.append(items)
def get_Html(self, url, methods, res, headers, encoding, params=None):
# 判断是否使用本地代{过}{滤}理外的代{过}{滤}理
if len(self.proxy_list) == 0: proxy = None
else: proxy = eval(self.proxy_list[randint(0, len(self.proxy_list)-1)])
# 获取text,json格式
if params:
if methods == 'GET': data = requests.get(url=url, headers=headers, params=params, proxies=proxy)
elif methods == 'POST': data = requests.post(url=url, headers=headers, data=params, proxies=proxy)
else:
if methods == 'GET': data = requests.get(url=url, headers=headers, proxies=proxy)
elif methods == 'POST': data = requests.post(url=url, headers=headers, proxies=proxy)
data.encoding = encoding
if res == 'text': return data.text
elif res == 'json': return data.json()
|