python爬虫练习案例:最受欢迎的影评数据保存csv文件

代码如下

import re
from urllib.request import urlopen,Request
import xlwt


url="https://movie.douban.com/review/best/"
headers = {'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Mobile Safari/537.36 Edg/92.0.902.55'}
webSourceCode=urlopen(Request(url, headers=headers)).read().decode("utf-8","ignore")
# 突出评论
titleRe=re.compile(r'<h2><a href=".*?">(.*?)</a></h2>')
# 作者
authorRe=re.compile(r'<a href=".*?" class="name">(.*?)</a>')
# 时间
timeRe=re.compile(r'<span content=".*?" class="main-meta">(.*?)</span>')
# 简要
contentRe=re.compile(r'<div class="short-content">.*?(.*?)&nbsp',re.S)
wordRe=re.compile(r'<a class="subject-img" href="(.*?)">(.*?)<img alt="(.*?)" title="(.*?)" src="(.*?)" rel="v:image" />(.*?)</a>')
nameRe=re.compile(r'<a class="subject-img" href="(.*?)"><img alt="(.*?)" title="(.*?)"(.*?)>(.*?)</a>')

names=nameRe.findall(webSourceCode)
times=timeRe.findall(webSourceCode)
words=wordRe.findall(webSourceCode)
titles=titleRe.findall(webSourceCode)
content=contentRe.findall(webSourceCode)
authors=authorRe.findall(webSourceCode)
print(nameRe)
work_book = xlwt.Workbook(encoding='utf-8')
sheet = work_book.add_sheet('豆瓣影评',cell_overwrite_ok=True)
sheet.write(0, 0, '影片名称')
sheet.write(0, 1, '时间')
sheet.write(0, 2, '突然评论')
sheet.write(0, 3, '网站地址')
sheet.write(0, 4, '内容介绍')
sheet.write(0, 5, '图片网站')
row_num1 = 1
row_num2 = 1
row_num3 = 1
row_num4 = 1
row_num5 = 1
row_num6 = 1
print("时间==============================================================")
for time in times:
    sheet.write(row_num1, 1, time)
    row_num1 += 1
print("突出评论==============================================================")
for title in titles:
    print(title)
    sheet.write(row_num2, 2, title)
    row_num2 += 1
print("内容简介==============================================================")
for c in content:
    print(c)
    sheet.write(row_num4, 4, c)
    row_num4 += 1
print("网站==============================================================")
for word in words:
    print(word[0])
    sheet.write(row_num3, 3, word[0])
    row_num3 += 1
print("名称==============================================================")
for name in words:
    print(name[3])
    sheet.write(row_num6, 0, name[3])
    row_num6 += 1
print("图片==============================================================")
for img in words:
    print(img[4])
    sheet.write(row_num5, 5, img[4])
    row_num5 += 1
# 将工作表,excel文件,保存到本地路径
file_name = r'C:\Users\admin\PycharmProjects\pythonProject\venv\Include\data2.xls'
work_book.save(file_name.encode("utf-8").decode("utf-8"))

代码运行结果:


时间==============================================================

突出评论==============================================================

内容简介==============================================================
                       
网站==============================================================

名称==============================================================

图片==============================================================


Process finished with exit code 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西皮树下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值