ceshi

60 阅读1分钟

import requests import json import logging import os

try: log_dir = os.path.dirname('c:\code\demo\spider_debug.log') if not os.path.exists(log_dir): os.makedirs(log_dir) logging.basicConfig(level=logging.INFO, filename='c:\code\demo\spider_debug.log', filemode='w', format='%(asctime)s - %(levelname)s - %(message)s') except Exception as log_error: print(f'日志文件配置失败: {log_error}')

def fetch_questions(): logging.info('开始获取问题数据') api_url = "opentdb.com/api.php" params = { "amount": 100, "category": 18, # IT相关类别 "type": "multiple", "difficulty": "medium" } logging.info('发送请求到 API: %s,参数: %s', api_url, params) response = requests.get(api_url, params=params) logging.info('收到 API 响应,状态码: %s', response.status_code) if response.status_code == 200: data = response.json() questions = [] for item in data.get('results', []): question = { 'title': item.get('question'), 'options': item.get('incorrect_answers') + [item.get('correct_answer')], 'answer': item.get('correct_answer') } questions.append(question) return questions else: logging.error('获取数据失败,状态码: %s', response.status_code) print("Failed to fetch data") return []

def main(): logging.info('程序启动') try: logging.info('调用 fetch_questions 函数') questions = fetch_questions() logging.info('尝试打开 questions.json 文件进行写入') with open('questions.json', 'w', encoding='utf-8') as f: logging.info('将 %d 条问题数据写入文件', len(questions)) json.dump(questions, f, ensure_ascii=False, indent=4) logging.info('已爬取 %d 条问题', len(questions)) print(f"Scraped {len(questions)} questions.") logging.info('数据已保存到 questions.json') print("Data saved to questions.json") except Exception as e: logging.error('程序运行过程中发生错误: %s', e) print(f"An error occurred: {e}")

import sys if name == "main": try: main() except Exception as global_e: print(f'全局异常捕获: {global_e}') if hasattr(logging, 'error'): logging.error(f'全局异常捕获: {global_e}') sys.exit(1)

网站; www.51test.net/show/984893…