获取内容资料
Python开发

python爬去网易云课堂

地址如下: study.163.com/courses/

我简单的看了一下,页面数据是基于 https://study.163.com/p/search/studycourse.json 这个地址进行异步加载的。你自己尝试的时候需要借助开发者工具 进行多次尝试,抓取到这个地址的数据为准。

还有一个地方需要注意,这次是post提交方式,并且提交数据是payload类型的,这个原因导致我们的代码和以前的略微有一些不同的地方。

python爬去网易云课堂

提取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门课程

好了,需要代码和数据,请评论留下我能联系你的方式即可。

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注