# Author:KRL
# -*- codeing = utf-8 -*-
# @Time :2021/7/3020:13
# @Author :MI
# @Site :
# @File :doubantop250.py
# @Software :PyCharm
# 拿到网页源代码 requests
# 利用re提取我们需要的内容 re
import requests
import re
import csv
url = "https://movie.douban.com/top250"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}
resp = requests.get(url,headers=headers)
page_content = resp.text
# print(resp.text)
# 解析数据
obj = re.compile(r'<li>.*? <div class="item">.*?<span class="title">(?P<name>.*?)</span>.*?'
r'<p class="">(?P<director>.*?); (?P<actors>.*?)<br>(?P<years>.*?)'
r' / (?P<country>.*?) / (?P<style>.*?)</p>.*?'
r'<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?'
r'<span>(?P<Number_of_comments>.*?)</span>.*?<span class="inq">(?P<motto>.*?)</span>',re.S)
# 开始匹配
result = obj.finditer(page_content)
# 写入CSV 为数据分析做准备
f = open("data.csv",mode="w",encoding='utf-8')
csvwriter = csv.writer(f)
for it in result:
# print(it.group("name"))
# print(it.group("director").strip())
# print(it.group("actors"))
# print(it.group("years").strip())
# print(it.group("country"))
# print(it.group("style").strip())
# print(it.group("score").strip())
# print(it.group("Number_of_comments"))
# print(it.group("motto"))
dic = it.groupdict()
dic['director'] = dic['director'].strip()
dic['years'] = dic['years'].strip()
dic['style'] = dic['style'].strip()
dic['score'] = dic['score'].strip()
csvwriter.writerow(dic.values())
f.close()
print('done!')
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术