Python爬虫入门教程 21-100 网易云课堂课程数据抓取
2019-01-09 10:13
1016 查看
写在前面
今天咱们抓取一下网易云课堂的课程数据,这个网站的数据量并不是很大,我们只需要使用
requests就可以快速的抓取到这部分数据了。
你第一步要做的是打开全部课程的地址,找出爬虫规律,
地址如下:
https://study.163.com/courses/
我简单的看了一下,页面数据是基于
https://study.163.com/p/search/studycourse.json这个地址进行异步加载的。你自己尝试的时候需要借助
开发者工具
进行多次尝试,抓取到这个地址的数据为准。
还有一个地方需要注意,这次是
post提交方式,并且提交数据是
payload类型的,这个原因导致我们的代码和以前的略微有一些不同的地方。
提取post关键字,看一下各个参数的意思,如果你爬取的网站足够多,那么训练出来的敏感度能够快速的分析这些参数
{"pageIndex":55, # 页码 "pageSize":50, # 每页数据大小 "relativeOffset":2700, "frontCategoryId":-1, "searchTimeType":-1, "orderType":50, "priceType":-1, "activityId":0, "keyword":"" # 搜索相关 }
好了,可以开始编写代码了,核心的代码就是通过
requests模块发送
post请求
def get_json(index): print(f"正在抓取{index}页数据") payload = {"pageIndex":index, "pageSize":50, "relativeOffset":50, "frontCategoryId":-1, "searchTimeType":-1, "orderType":50, "priceType":-1, "activityId":0, "keyword":"" } headers = {"Accept":"application/json", "Host":"study.163.com", "Origin":"https://study.163.com", "Content-Type":"application/json", "Referer":"https://study.163.com/courses", "User-Agent":"自己去找个浏览器UA" } try: # 请注意这个地方发送的是post请求 # CSDN 博客 梦想橡皮擦 res = requests.post("https://study.163.com/p/search/studycourse.json",json=payload,headers=headers) content_json = res.json() if content_json and content_json["code"] == 0: data = get_content(content_json) # 获取正确的数据 ############################################ if len(data) > 0: save_mongo(data) # 保存数据 ############################################ except Exception as e: print("出现BUG了") print(e) finally: time.sleep(1) index+=1 get_json(index) def get_content(content_json): if "result" in content_json: return content_json["result"]["list"]
因为获取到的数据是json类型的,所以,数据可以快速的保存到
mongodb里面,保存数据的代码我依旧留空,希望你自己可以完善。
通过很短的时间,我们就捕获到了
3000门课程
好了,需要代码和数据,请评论留下我能联系你的方式即可。
相关文章推荐
- Python爬虫入门教程 16-100 500px摄影师社区抓取摄影师数据
- Python爬虫入门教程 19-100 51CTO学院IT技术课程抓取
- Python爬虫入门教程 17-100 博客抓取数据
- Python爬虫入门教程 15-100 石家庄政民互动数据爬取
- Python爬虫入门教程 18-100 煎蛋网XXOO图片抓取
- Python爬虫入门教程 3-100 美空网数据爬取
- Python爬虫入门教程 10-100 图虫网多线程爬取
- Python爬虫入门教程 8-100 蜂鸟网图片爬取之三
- Python爬虫入门教程 5-100 27270图片爬取
- Python爬虫入门教程 1-100 CentOS环境安装
- Python爬虫入门教程 2-100 妹子图网站爬取
- Python爬虫入门教程 10-100 图虫网多线程爬取!
- python爬虫"Hello World"级入门实例(二),使用json从中国天气网抓取数据
- Python爬虫入门教程 13-100 斗图啦表情包多线程爬取
- Python数据挖掘课程 一.安装Python及爬虫入门介绍
- Python爬虫入门教程 12-100 半次元COS图爬取
- Python爬虫入门教程 14-100 All IT eBooks多线程爬取
- 【Python高级工程师之路】入门+进阶+实战+爬虫+数据分析整套教程
- Python爬虫入门教程 4-100 美空网未登录图片爬取
- Python爬虫入门教程 11-100 行行网电子书多线程爬取