1 import requests 2 3 4 url ="https://book.douban.com/subject/1084336/comments/" 5 response = requests.get(url) 6 r = response.text 7 8 from bs4 import BeautifulSoup 9 soup = BeautifulSoup(r,"lxml") 10 pattern = soup.find_all("p","comment-content") 11 12 for item in pattern : 13 print(item.text) 14 with open("xiaoshuo.txt","a+",encoding="utf-8") as f : 15 f.write(item.text) 16 f.close() 17 18 19 20 21 # comments = [] 22 # for item in pattern : 23 # comments.append(item.text) 24 # 25 # df = pandas.DataFrame(comments) 26 # df.to_csv("comment.csv")