首先安装requests库和 bs4库
# 获取页面
link ="http://www.santostang.com/"
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
r = requests.get(link,headers=headers)
soup = BeautifulSoup(r.text,"html.parser")
title = soup.find("h1",class_="post-title").a.text.strip()
print(title)
# 保存到文件
with open("title.txt","a+") as f:
f.write(title)
f.close()