Files
nanocode/it_automation_test/scripts/simple_crawler_test.py
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

196 lines
5.7 KiB
Python

#!/usr/bin/env python3
"""
简单网页抓取测试
测试网络连接和基本功能
"""
import requests
import sqlite3
import os
import datetime
def test_network_connection():
"""测试网络连接"""
print("🌐 测试网络连接...")
test_urls = [
"https://httpbin.org/get", # 测试网站
"https://api.github.com", # GitHub API
"https://jsonplaceholder.typicode.com/posts/1" # 测试API
]
for url in test_urls:
try:
print(f" 测试连接: {url}")
response = requests.get(url, timeout=10)
if response.status_code == 200:
print(f" ✅ {url} - 连接成功 (状态码: {response.status_code})")
return True
else:
print(f" ⚠️ {url} - 连接异常 (状态码: {response.status_code})")
except Exception as e:
print(f" ❌ {url} - 连接失败: {e}")
return False
def test_database():
"""测试数据库功能"""
print("\n🗄️ 测试数据库功能...")
db_path = "/home/ubuntu/nanocode/it_automation_test/data/test_simple.db"
os.makedirs(os.path.dirname(db_path), exist_ok=True)
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 创建测试表
cursor.execute('''
CREATE TABLE IF NOT EXISTS test_table (
id INTEGER PRIMARY KEY,
name TEXT,
value INTEGER,
created_at TIMESTAMP
)
''')
# 插入测试数据
test_data = [
("测试1", 100, datetime.datetime.now().isoformat()),
("测试2", 200, datetime.datetime.now().isoformat()),
("测试3", 300, datetime.datetime.now().isoformat()),
]
cursor.executemany('INSERT INTO test_table (name, value, created_at) VALUES (?, ?, ?)', test_data)
conn.commit()
# 查询数据
cursor.execute('SELECT COUNT(*) FROM test_table')
count = cursor.fetchone()[0]
print(f" ✅ 数据库创建成功: {db_path}")
print(f" ✅ 插入 {count} 条测试数据")
# 显示数据
cursor.execute('SELECT name, value FROM test_table ORDER BY id')
print(" 📊 测试数据:")
for row in cursor.fetchall():
print(f" • {row[0]}: {row[1]}")
conn.close()
return True
except Exception as e:
print(f" ❌ 数据库测试失败: {e}")
return False
def test_requests_library():
"""测试requests库功能"""
print("\n📡 测试requests库功能...")
try:
# 测试简单的GET请求
test_url = "https://httpbin.org/json"
response = requests.get(test_url, timeout=10)
if response.status_code == 200:
data = response.json()
print(f" ✅ GET请求成功: {test_url}")
print(f" 📄 响应类型: {type(data)}")
print(f" 🔤 示例数据: {str(data)[:100]}...")
return True
else:
print(f" ❌ GET请求失败: 状态码 {response.status_code}")
return False
except Exception as e:
print(f" ❌ requests库测试失败: {e}")
return False
def test_beautifulsoup():
"""测试BeautifulSoup功能"""
print("\n🍲 测试BeautifulSoup功能...")
try:
from bs4 import BeautifulSoup
# 创建测试HTML
test_html = """
<html>
<head><title>测试页面</title></head>
<body>
<h1>标题1</h1>
<p class="content">段落1</p>
<p class="content">段落2</p>
<a href="https://example.com">链接</a>
</body>
</html>
"""
soup = BeautifulSoup(test_html, 'html.parser')
# 测试选择器
title = soup.title.string
paragraphs = soup.find_all('p', class_='content')
links = soup.find_all('a')
print(f" ✅ BeautifulSoup初始化成功")
print(f" 📝 标题: {title}")
print(f" 📄 段落数: {len(paragraphs)}")
print(f" 🔗 链接数: {len(links)}")
return True
except Exception as e:
print(f" ❌ BeautifulSoup测试失败: {e}")
return False
def main():
"""主测试函数"""
print("=" * 60)
print("🧪 开始基础功能测试")
print("=" * 60)
tests_passed = 0
total_tests = 4
# 运行测试
if test_network_connection():
tests_passed += 1
if test_database():
tests_passed += 1
if test_requests_library():
tests_passed += 1
if test_beautifulsoup():
tests_passed += 1
# 测试结果
print("\n" + "=" * 60)
print("📊 测试结果汇总")
print("=" * 60)
print(f"✅ 通过的测试: {tests_passed}/{total_tests}")
if tests_passed == total_tests:
print("🎉 所有基础功能测试通过!")
print("\n💡 建议:")
print("1. 网站抓取失败可能是目标网站的反爬机制")
print("2. 可以尝试其他网站或添加更多请求头")
print("3. 基础库功能正常,可以进行下一步测试")
return True
else:
print("⚠️ 部分测试未通过")
print(f"❌ 失败的测试: {total_tests - tests_passed}")
return False
if __name__ == '__main__':
try:
success = main()
exit(0 if success else 1)
except Exception as e:
print(f"\n❌ 测试过程中出现未预期的错误: {e}")
import traceback
traceback.print_exc()
exit(1)