1. 爬取结果(csv文件,出现了有两个表头…不明所以,无关大雅)
2. 使用fiddler4进行抓包
通过观察url,我们不难发现其中的规律,要实现进行分类抓取,需要更改url第一个数字,如下
https://sc.canrike.com/Categories/1/hot/1.html
https://sc.canrike.com/Categories/2/hot/1.html
要实现翻页需要更改url的最后一个数字,如下
https://sc.canrike.com/Categories/2/hot/1.html
https://sc.canrike.com/Categories/2/hot/2.html
要实现抓取某个分类中的分类(就是最热,最新,榜单,完结),需要修改url的关键字
3. 先抓取分类里面的Id
# 构建请求头
headers = {
'Accept-Language': 'zh-CN,zh;q=0.8',
'User-Agent': 'okhttp-okgo/jeasonlzy',
'Content-Type': 'application/json',
'Host': 'sc.canrike.com',
'Connection': 'Keep-Alive',
'Accept-Encoding': 'gzip',
}
# 抓取Id
def get_Id():
url = 'https://sc.canrike.com/BookCategory.html'
# 请求url,返回的数据是json格式
resp = requests.get(url, headers=headers).json()
# 判断resp是否存在
if resp:
# 取出返回json数据里面的data键值对,data是一个列表
# 里面有我们需要的Id,通过Id来获取后面我们需要的信息
Ids = resp.get('data')
for temp in Ids:
Id = temp.get('Id')
yield Id
4. 先写获取最热书籍的信息,后面最新,榜单,完结的都是一样的代码,只需要更改url里面的关键字(比如hot,new,vote,over这些)。
# 获取最热的书籍信息
def get_hot_info(Id):
page = 1
while True:
time.sleep(1)
url = 'https://sc.canrike.com/Categories/{0}/hot/{1}.html'.format(Id, page)
resp = requests.get(url, headers=headers)
if resp.status_code == 200:
try:
data = json.loads(resp.content.decode(