目标网址:https://www.gushiwen.cn/
截图:
本代码可能存在些许问题,如个别古诗无法正常保存,测试案列皆可正常保存并排版,
下面直接上代码,复制粘贴可食用(偷懒就没写注释了):
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36'}
print("注意:本程序仅下载以关键字搜索到的第一篇古诗文!!!")
name = input("请输入要查找的古诗文关键字:")
url = "https://so.gushiwen.cn/search.aspx?value={}".format(name)
req = requests.get(url,headers=headers)
soup = BeautifulSoup(req.text,'lxml')
url = "https://so.gushiwen.cn"+soup.select("body>div>div>div>div>p>a")[0].get("href")
req = requests.get(url,headers=headers)
soup = BeautifulSoup(req.text,'lxml')
contents = soup.find("div",class_="contson").find_all("p")
if not bool(contents):
contents = soup.find("div",class_="contson")
text = name+"\n"+soup.find("p",class_="source").text.replace("\n","")+"\n"
for i in contents:
text = text+str(i).replace("<br/>","\n").replace("<p>","").replace("</p>","\n")
with open(name+'.txt','w') as fp:
fp.write(text)
print("下载完成!!!")