给博客点赞需要在登录的前提下才能实现。在浏览器操作点赞,利用浏览器自带的工具抓包。
1、获取请求的url:
2、Cookies,整段复制下来
3、用户代理
4、将上述的url,cookies,agent 填入程序中。 (不填入agent,也可以)
#coding=utf-8
import requests
#登录后才能访问的接口
url = 'https://blog.csdn.net/henry_lin_wind/phoenix/article/digg?ArticleId=89467959'
#浏览器登录后得到的cookie
cookie_str = r'填入你的cookie'
#设置请求头
headers = {'User-agent':'你的用户代理'}
#把cookie字符串处理成字典,以便接下来使用
cookies = {}
for line in cookie_str.split(';'):
key, value = line.split('=', 1)
cookies[key] = value
#发送点赞请求
resp = requests.get(url, headers = headers, cookies = cookies)
print(resp.content.decode('utf-8'))
返回结果:
点赞成功:{"status":true,"digg":1,"bury":"0"}
取消点赞:{"status":false,"digg":0,"bury":"0"}
5、批量点赞
根据用户名获取该用户的所有文章url,提取文章id。获取文章url参考我的博客https://blog.csdn.net/Henry_Lin_Wind/article/details/90295134
url_list = al.get_article_list('Henry_Lin_Wind')
#获取博客的地址
for article_name in url_list.keys():
article_url = url_list[article_name]
print(article_name)
print(article_url)
#从文章的url中提取文章id
article_id =re.search(r'details/[0-9]+',article_url).group().replace('details/','')
url_zan = 'https://blog.csdn.net/henry_lin_wind/phoenix/article/digg?ArticleId=' + article_id
print(url_zan)
#在发送get请求时带上请求头和cookies
resp = requests.get(url_zan, headers = headers, cookies = cookies)
print(resp.content.decode('utf-8'))
time.sleep(1)
print('--------------------')