SAE 部署 bilibili 爬虫

layout: post
title: sae部署bilibili爬虫
categories: python
tags: spider sae

前端效果

pic1

sae准备工作

从本地上传的爬虫到sae一直被提示没有requests模块,在requirements.txt里声明了也不行。起初我以为是这个第三方包被屏蔽了,直到看到sae支持中心-Python共享服务器-运行环境才直到原因:requirements.txt 只在容器云app里面才会生效,同理 runtime.txt 也是。因此需要在共享服务器上面使用第三方包的话,只能自己上传。我通过 ubantu python 2.7.6 使用pip install -t vendor requests安装上 requests 包,再将这个文件夹上传到 sae 根目录就可以使用了。因为涉及到我自己账号的 cookie,这里我使用的是码云私有仓库保存我的代码,不再贴出。

bilibili-Getcoin

B站只要登录一下就可以获得当日的一硬币,在爬虫界这算是非常简单的了。我之前尝试用国外的某ae来爬B站,得到503错误(403?),国外访问B站是要梯子的。
**code: **

# -*- coding: utf-8 -*-
"""
requests学习实战
"""
import requests
url = 'https://account.bilibili.com/site/getCoin'

headers = {}
cookies = {}
with requests.Session() as s:
r = s.get(url,headers=headers,cookies=cookies)
print r.status_code
bjson = r.json()
#print bjson.keys()
print bjson[u'data'] 

print 'over!'

以上。

2018/9/3 更新

cookies 需要每月更新。本月更新后无法获取硬币了,对比之前的cookies,发现本次更新后多出一个_jct键。现在删除后等待观察明天的结果。

结果仍然是不能获取。

原文地址:https://www.cnblogs.com/aubucuo/p/spider6.html