import urllib.request
import socket
import time
import sys
import re
import gzip
from io import BytesIO
import configparser
import os
config_file_path = 'config.ini'
open_count = 0
delay_time = 0
page_id = 0
cookie = ""
def createConfig():
print('\n[提示] 未发现配置文件, 创建中')
open_count = int(input("\n[提示] 请输入访问次数: "))
delay_time = int(input("\n[提示] 请输入每次访问间隔时间(秒): "))
page_id = int(input("\n[提示] 请输入页面ID: "))
cookie = input("\n[提示] 请输入浏览器cookie: ")
config = configparser.RawConfigParser()
config['Settings'] = {
'OpenCount': str(open_count),
'DelayTime': str(delay_time),
'PageId': str(page_id),
}
config['Request'] = {
'Cookie':fr'{cookie}'
}
with open('config.ini', 'w') as configfile:
config.write(configfile)
def loadConfig():
try:
print('\n[提示] 正在读取配置文件')
config = configparser.RawConfigParser()
config.read(config_file_path)
global open_count
open_count = int(config['Settings']['OpenCount'])
print(f'[提示] 访问次数: {open_count}')
global delay_time
delay_time = int(config['Settings']['DelayTime'])
print(f'[提示] 等待时间: {delay_time}')
global page_id
page_id = int(config['Settings']['PageId'])
print(f'[提示] 页面ID: {page_id}')
global cookie
cookie = config['Request']['Cookie']
print(rf'[提示] Cookie: {cookie}')
except FileNotFoundError:
print(f"[Error] 配置文件未找到.")
except Exception as e:
print(f"[Error] {e}")
def request():
url = f'https://www.7risha.com/{page_id}.html'
req_timeout = 60
for i in range(open_count):
print('\n[提示] 正在第' + str(i + 1) + '次请求地址: ' + url)
req_header = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
'Accept':'*/*',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8',
'Cookie':rf'{cookie}'
}
socket.setdefaulttimeout
try:
req = urllib.request.Request(url = url, data = None, headers = req_header)
resp = urllib.request.urlopen(url = req, data = None, timeout = req_timeout)
compressed_data = resp.read()
html = parstHtml(compressed_data)
match = re.search(r'<span>阅读\S*\s*\S*</span>', html)
if match:
read_count = match.group(0).replace("<span>", '').replace("</span>", '')
print('[Success] ' + read_count)
else:
print('[Success] 阅读: 未知')
except urllib.error.URLError as e:
print(f'[Error] {e}')
except Exception as e:
print(f'[Error] {e}')
i + 1
time.sleep(delay_time)
def parstHtml(compressed_data):
with gzip.GzipFile(fileobj=BytesIO(compressed_data)) as f:
uncompressed_data = f.read()
return uncompressed_data.decode('utf-8')
if __name__ == "__main__":
sys.stdin = open(sys.stdin.fileno(), mode='r', encoding='utf-8', buffering=True)
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf-8', buffering=True)
print('[提示] 脚本启动')
if not os.path.exists(config_file_path):
createConfig()
loadConfig()
request()