import requests from bs4 import BeautifulSoup link = "http://www.santostang.com" header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 SE 2.X MetaSr 1.0" } response = requests.get(link,headers = header) # print(response.text) soup = BeautifulSoup(response.text,"html.parser") title = soup.find("h1",class_ = "post-title").a.text.strip() #参数class_带下划线 print(title) with open("ceshi.txt","a+") as f: f.write(title)