当前位置:网站首页>What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
2022-08-05 08:08:00 【This product brother】
Requirements Analysis
As we all know, today is the Qixi Festival.Those who can live the festival must have gone out to play and on the way to go out to play.In the afternoon, I also received a guide "How to shorten the distance between couples watching horror movies on Qixi Festival", unfortunately, although the guide is good, it seems that I can't use it
You can't go out and play without watching a movie, right?So, which movie is better to watch?Presumably many couples may not pay attention to the news of movies on weekdays. When they are looking for a movie to watch, they don't know what to watch.Maybe, I heard that the recent movie called "Lone on the Moon" has exceeded the box office of hundreds of millions. Should you watch this movie?
I don't know if you ask me.However, if you want to know which movies are better to watch, a good way is to check the ratings of the movies, so that you can see at a glance?Although a movie with a high rating does not mean that it is good, at least it means that most people like to watch it, right?Just do it, today we will crawl the ratings of the movie
Implementation Analysis
Crawler programs are like this. If there is anti-crawling, it will be more difficult.It would be easier if there was no back-climbing.In terms of data extraction, if the structure is complex and the required content is messy, it will be difficult to extract. If it is structured data, such as an ajax request, it will be very easy to get json data directly
It just so happens that today's movie ratings have neither anti-climbing nor data in json format, so they can be easily extracted at once, very easy.Of course, this is not our credit, mainly because the other party's website is more generous
As for the reading and writing of csv, it is also very easy, and there are many ways to achieve it.For example, you can read and write files by yourself, and read and write files in csv format.For example, read and write csv through a third-party library that can operate csv.Of course, there is a simpler way to directly use the built-in csv module in python.Although the function is not as powerful as the third-party library, it is also a good choice
Of course, there may be some friends who may not understand the csv format.In fact, csv is similar to excel, and it can also be made into this format of excel, but it is relatively simple. If you don't know this, you can learn about it separately
import csv # Import the built-in csv module# Through the context manager, open a file normallywith open("douban.csv", "w", newline="") as csvfile:# Through csv.writer(), create a writer for subsequent writing to the csv filew = csv.writer(csvfile, delimiter=",")# Through the method of writerow(), write the row of csvw.writerow(["title", "rating", "address", "picture"])for row in r.json()["subjects"]:w.writerow([row["title"], row["rate"], row["url"], row["cover"]])
In addition, some friends may also be confused.If you want to read and parse json files, shouldn't you import the json module?Why is this not used at all in the code below?
In fact, in requests, the json module is already included, and there is a simpler way to use it, so you can use json directly through requests.There is no need to introduce additional built-in modules for json
Complete code implementation
import requestsimport csvfrom base64 import b64decodeheaders = {"user-agent": "Mozilla/5.0"}url = b64decode("aHR0cHM6Ly9tb3ZpZS5kb3ViYW4uY29tL2ovc2VhcmNoX3N1YmplY3RzP3R5cGU9bW92aWUmdGFnPeeDremXqCZzb3J0PXJlY29tbWVuZCZwYWdlX2xpbWl0PTIwJnBhZ2Vcode).defr = requests.get(url, headers=headers)with open("douban.csv", "w", newline="") as csvfile:w = csv.writer(csvfile, delimiter=",")w.writerow(["title", "rating", "address", "picture"])for row in r.json()["subjects"]:w.writerow([row["title"], row["rate"], row["url"], row["cover"]])
After the program runs, you can see that it has been successfully saved as a csv file, the effect is as follows
So, finally, I wish everyone a happy Qixi Festival. If you can celebrate the festival, you can go to the movies according to the ratings.If you can't have a festival.Well, well, you can also go to the movies by yourself, otherwise, if you don't go out, won't everyone know that you have no festivals!
边栏推荐
猜你喜欢
Fiddler tool explanation
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
Chapter3、色调映射
数据库——概述
egg框架中解决跨域的三种方案
Discourse 清理存储空间的方法
游戏模拟器成了外挂帮凶,灰产对抗再升级
SVG big fish eat small fish animation js special effects
YOLOv3 SPP理论详解(包括CIoU及Focal loss)
uniapp time component encapsulates year-month-day-hour-minute-second
随机推荐
漂亮MM和普通MM的区别
Redis常用命令
SVG big fish eat small fish animation js special effects
512色色谱图
【 LeetCode 】 235. A binary search tree in recent common ancestor
微信 小程序 之PC端 不支持 wx.previewMedia 方法 故用自定义轮播图进行 模拟照片视频的播放
【结构体内功修炼】枚举和联合的奥秘(三)
别把你的天使弄丢了
利用Jenkins的持续集成
YOLOv3 SPP理论详解(包括CIoU及Focal loss)
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
小本创业者的致胜法宝!
标准C语言15
爬虫之验证码
行业应用软件项目经理三步曲
谷歌零碎笔记之MVCC(草稿)
Use of thread pool (combined with Future/Callable)
高端无主灯设计灯光设计该如何布置射灯灯具?
达梦数据库大表添加字段
MongoDB 语法大全