
本文介绍使用 python(requests + beautifulsoup)批量抓取 biblestudytools 网站《smith’s bible dictionary》中人名定义的完整方案,重点解决动态匹配 `` 标签内目标文本、异常处理与结果结构化存储问题。
在批量网络爬虫实践中,仅依赖 soup.find('i')(返回首个 标签)极易导致数据遗漏或错配——因为目标定义通常嵌套在多个 标签中,且并非总位于页面第一个。例如,Aaron 页面 的定义 “a teacher, or lofty” 实际位于段落末尾的 标签内,而非开头。
正确做法是:先定位所有 标签,再逐个检查其文本内容是否语义相关。由于定义文本通常包含对人名的解释性短语(如 “means”, “signifies”, “denotes”, 或直接以逗号分隔的释义),而不仅仅是精确匹配人名字符串,原答案中 if name in i.text 的逻辑存在误判风险(例如 abednego 出现在链接 URL 中,但未必出现在 文本里)。更稳健的策略是:提取 标签中紧邻 标题后的首段释义,或匹配含常见定义动词的 文本
。
以下是优化后的完整实现:
import requests
from bs4 import BeautifulSoup
import time
# 假设 test 是已有的名字列表,如 ['aaron', 'abednego', ...]
test = ['aaron', 'abednego'] # 替换为你的实际列表
smiths_names = {}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
for name in test:
url = f"https://www.biblestudytools.com/dictionaries/smiths-bible-dictionary/{name.lower()}.html"
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # 抛出 HTTP 错误(如 404)
soup = BeautifulSoup(response.content, 'html.parser')
# 策略1:优先查找 后紧跟的
内的 (常见结构)
h1 = soup.find('h1')
if h1:
next_p = h1.find_next('p')
if next_p:
itag_in_p = next_p.find('i')
if itag_in_p:
meaning = itag_in_p.get_text(strip=True)
smiths_names[name] = meaning
print(f"✓ {name}: {meaning[:60]}...")
continue
# 策略2:遍历所有 ,匹配含定义关键词的文本
itags = soup.find_all('i')
for i_tag in itags:
text = i_tag.get_text(strip=True)
if text and any(kw in text.lower() for kw in ['means', 'signifies', 'denotes', 'i.e.', 'that is']):
smiths_names[name] = text
print(f"✓ {name}: {text[:60]}...")
break
else:
# 策略3:回退到第一个非空 (兜底)
first_i = soup.find('i')
if first_i and first_i.get_text(strip=True):
smiths_names[name] = first_i.get_text(strip=True)
print(f"⚠ {name}: using first as fallback")
else:
print(f"✗ {name}: no usable tag found")
except requests.exceptions.RequestException as e:
print(f"❌ {name} request failed: {e}")
except Exception as e:
print(f"❌ {name} parsing error: {e}")
time.sleep(1) # 尊重网站,避免请求过频
print(f"\n✅ Completed. Scraped {len(smiths_names)} definitions.")
关键改进说明:
- ✅ 三层容错机制:优先语义定位 → 关键词匹配 → 首标签兜底,显著提升成功率;
- ✅ 健壮异常处理:区分网络异常(超时/404)与解析异常,避免中断整个循环;
- ✅ 反爬友好:添加 User-Agent 头 + 请求间隔(time.sleep(1)),降低被封风险;
- ✅ 大小写安全:URL 构造时统一转小写(该网站路径为小写);
- ✅ 结果可验证:每步输出清晰日志,便于调试。
注意事项:
- 请务必遵守 robots.txt(https://www.php.cn/link/659b7cf906b8fd348ff333c167d8386d)及网站条款,建议仅用于个人学习、非商业用途;
- 若需长期稳定运行,建议增加重试机制(如 tenacity 库)和代理池支持;
- 定义文本可能含 HTML 实体(如 ),可用 html.unescape() 清洗;
- 最终字典 smiths_names 可导出为 JSON/CSV:
import json with open('smiths_definitions.json', 'w', encoding='utf-8') as f: json.dump(smiths_names, f, indent=2, ensure_ascii=False)
通过以上方法,你不仅能精准获取每个希伯来人名的权威释义,还能构建可复用、易维护的词典抓取流程。










