当前位置:网站首页>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!
边栏推荐
猜你喜欢

Adb authorization process analysis

egg框架中解决跨域的三种方案

Fiddler tool explanation

最 Cool 的 Kubernetes 网络方案 Cilium 入门教程

D2--FPGA SPI interface communication2022-08-03

ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色

MobileNetV1架构解析

双向循环带头链表

Adb 授权过程分析

Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
随机推荐
监听浏览器刷新操作
生命的颜色占卜
2006年星座运势全解-射手
P1103 书本整理
Use of thread pool (combined with Future/Callable)
TensorFlow installation steps
二叉树进阶复习1
Mysql 死锁和死锁的解决方案
外企Office常用英语
Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
国家强制性灯具安全标准GB7000.1-2015
作为一个男人必须明白的22个道理
JS实现从照片中裁切自已的肖像
Long-term recruitment embedded development-Shenzhen Baoan
CROS和JSONP配置
行走社会100绝招
奇怪的Access错误
爬虫从入门到入牢
MobileNetV2架构解析
字符串提取 中文、英文、数字