不知道为什么会这样,代码如下~
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")