import requests
from bs4 import BeautifulSoup
import csv
start = 0
result = []
f = open("置身世外评论500条.csv", 'w', encoding="utf-8-sig", newline="")
csvwriter = csv.writer(f)
for i in range(0, 3):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"
}
url = 'https://book.douban.com/subject/35481711/reviews?start=' + str(start) + '&limit=20&status=P&sort=new_score'
resp = requests.get(url, headers=headers)
resp.encoding = "utf-8"
# print(resp.text)
start += 20
soup = BeautifulSoup(resp.text, "html.parser")
for item in soup.find_all("div", "comment"):
comments = item.find("span", {"class": "short"}).string
name = item.find("span", "comment-info").a.string
vote = item.find("span", {"class": "vote-count"}).string
oneresult = [name, vote, comments]
result.append(oneresult)
# print(result)
csvwriter.writerow(['【' + name + '】', "👍" + vote + "👍", '{' + comments + '}'])
f.close()
print("over")
csv本身是不带格式的纯文本文件,你这个显示效果是excel打开后自动处理的
格式看起来没问题呀, 你用其他的文本编辑器打开看看, 比如 ue 或者 notepad ++