赞
踩
代码如下
- 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">.*?(.*?) ',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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。