python爬虫豆瓣高分电影前一百部
2020-03-18 19:02
127 查看
思路:
- 打开豆瓣相关页,进行抓包
- 抓取json的url,进行爬取
- headers,url,get或post
- 返回json后进行处理
处理json
- 显示采用jsonpath得到电影名
- 因为博主初学,还不会得到电影名与评分,故采取了两次jsonpath.jsonpath
- 得到之后的list进行交叉合并,此处使用的是chain
- 在合并后的list采取算法使其进行换行以及隔开
- 最后保存在本地
import requests import json import jsonpath from itertools import chain url = "https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&page_limit=100&page_start=0" headers = { "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Mobile Safari/537.36" } r = requests.get(url=url,headers=headers) # print(r.content.decode()) ret = json.dumps(r.content.decode(),ensure_ascii=False,indent=4) # print(type(r.content.decode())) with open("douban.json","w",encoding="utf-8") as f: f.write(ret) # # with open("douban.json","r",encoding="utf-8") as f: # ret4 = json.load(f) # print(ret4) # print(type(ret4)) # print(r.json()) # res = r.json()['subjects'][0]['title'] # print(r.json()['subjects'][0]['title']) # print(type(res)) # print(r.json()) # print(type(r.json())) name = jsonpath.jsonpath(r.json(),'$..title') rate = jsonpath.jsonpath(r.json(),'$..rate') # print(name) # print(rate) # print(type(name)) want = list(chain.from_iterable(zip(name,rate))) # print(want) count1 = 0 for w in want: if count1%3 == 1: want.insert(count1,":") # elif count%2 == 0: # want.insert(count,"\n") # print() count1 += 1 count2 = 0 for w in want: if count2%4 == 0: want.insert(count2,"\n") # elif count%2 == 0: # want.insert(count,"\n") # print() count2 += 1 print(want) str1 = " ".join(want) print(str1) with open("want.txt","w",encoding="utf-8") as f: f.write(str1)
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
- Python爬虫之多线程下载豆瓣Top250电影图片
- 运维学python之爬虫高级篇(五)scrapy爬取豆瓣电影TOP250
- python 爬虫 进程池 豆瓣电影250
- 【Python简单爬虫设计】对豆瓣TOP100的电影名及简要的爬取
- 一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息
- Python爬虫实战(4)豆瓣电影
- python3爬虫豆瓣top250电影(并保存到mysql数据库)
- Python爬虫学习笔记 -- 爬取豆瓣电影top250的信息
- 爬虫第二篇--豆瓣电影(Python)
- python爬虫之通过正则表达式获取豆瓣最新上映电影的海报
- Python 爬虫个人记录(一)豆瓣电影250
- Python爬虫入门——2. 4 利用正则表达式爬取豆瓣电影 Top 250
- [Python/爬虫]利用xpath爬取豆瓣电影top250
- Python 爬虫实战(1):分析豆瓣中最新电影的影评并制作词云
- Python爬虫实战之豆瓣音乐、微打赏、阳光电影(附代码)
- Python爬虫实战(一):爬取豆瓣电影top250排名
- python爬虫之豆瓣电影评分
- Python爬虫实现的根据分类爬取豆瓣电影信息功能示例
- python爬虫——爬取豆瓣电影top250信息并加载到MongoDB数据库中