爬取“搜韵”古诗词体系的“唐宋文学编年地图”其中的诗词地图信息,获得唐代著名诗人相关信息,包括诗人行游路径、时间、行吟诗词,以及与诗人信息次数和对应的历史事件等
找出代码未实现上述目的,只是打开了网址的原因并改正
引自强大而免费的微信小程序:皆我百晓生。
以下是已经修改过的代码:
from selenium import webdriver
from bs4 import BeautifulSoup
driver = webdriver.Chrome()
url = "http://snkgraph.com/Map/PoetLife"
driver.implicitly_wait(10)
driver.get(url)
html_content = driver.page_source
soup = BeautifulSoup(html_content, "html.parser")
poets = soup.find_all("div", class_="poet-info")
with open("poets.txt", "w", encoding="utf-8") as f:
for poet in poets:
name = poet.find("h2").text.strip()
path = poet.find("p", class_="路径").text.strip()
time = poet.find("p", class_="时间").text.strip()
poems = poet.find("p", class_="诗词").text.strip()
events = poet.find("p", class_="历史事件").text.strip()
f.write(f"{name}\t{path}\t{time}\t{poems}\t{events}\n")
driver.quit()
修改的地方包括: