最近把 《琉璃》 给追完了,这部剧的特效我觉得很不错!就是太虐了,感觉男主太惨了!我看到一个弹幕说:“男主把剧组的血包都承包了!”戳中了我的笑点!
强行转入我要说的(哈哈)!对于一部作品,每个人都有不同的看法,我很好奇大家对这部剧的评价,所以我就想从豆瓣爬取大家得评价!(又强行因果,哈哈哈)
源码如下:
#目的: 爬取热播剧《琉璃》的短评
import requests
from lxml import etree
import xlwt
if __name__ == '__main__':
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
}
url = 'https://movie.douban.com/subject/30358061/comments?start=%d&limit=20&sort=new_score&status=P'
users = []
comments =[]
time = []
stars = []
o = 1
for i in range(0,201,20):
new_url = format(url%i)
# print(new_url)
page_text = requests.get(url=new_url,headers=headers).text
tree = etree.HTML(page_text)
users_list = tree.xpath('//div[@class="comment-item"]//a/@title')
users = users+users_list
comments_list = tree.xpath('//div[@class="comment-item"]//p/span/text()')
comments = comments+comments_list
time_list = tree.xpath('//*[@id="comments"]//h3/span[2]/span[3]/@title')
time = time+time_list
stars_list = tree.xpath('//*[@id="comments"]//h3/span[2]/span[2]/@title')
stars = stars+stars_list
print('第%d页爬取完成!!!'%o)
o = o+1
###################################################################################################
###################################################################################################
# 创建工作簿
f = xlwt.Workbook()
# 创建一个sheet
sheet1 = f.add_sheet('琉璃短评', cell_overwrite_ok=True)
col1 = sheet1.col(0)
col1.width = 256 * 38
col2 = sheet1.col(1)
col2.width = 256 * 85
col3 = sheet1.col(2)
col3.width = 256 * 85
# 初始化第一行(有三列)
row0 = [u'用户名', u'时间',u'星级',u'评论']
for i in range(0, len(row0)):
sheet1.write(0, i, row0[i])
# 初始化第一列数据
col1_data = users
for k in range(len(col1_data)):
sheet1.write(k+1, 0, col1_data[k])
# 初始化第二列数据
col2_data = time
for m in range(len(col2_data)):
sheet1.write(m+1, 1, col2_data[m])
# 初始化第三列数据
col3_data = stars
for l in range(len(col3_data)):
sheet1.write(l+1, 2, col3_data[l])
# 初始化第四列数据
col4_data = comments
for k in range(len(col4_data)):
sheet1.write(k+1, 3, col4_data[k])
# 保存文件
f.save("./琉璃短评.xls")
这是我第一次写文章,对爬虫也不是很熟练,如果对代码有什么修改建议,欢迎在评论区留言!!