csdn自动关注,就能搞到两千。
不是通过解析html,通过抓接口进行关注。
MySQL存入用户信息到数据库。
一、
二、代码
import time
import pymysql # pymysql数据库
from fake_useragent import UserAgent
import requests as request # 网络请求request
import json
userName = "" # 设置自己用户名称
token = "" # 查看网页中cookies中的token
class CsdnSpider(object):
def __init__(self):
global userName
global token
self.cookices = {
"UserName": userName, # 设置自己用户名称
"UserToken": token, # 设置登录token
}
self.us = UserAgent()
self.head = {
"User-Agent": self.us.random,
}
self.seesion = request.Session()
self.url = "https://blog.youkuaiyun.com//phoenix/web/v1/follow/list?page=1&pageSize=40&blogUsername="
self.help = MysqlHelp()
def parse_html(self, name):
json_str = self.get_url(name.replace(" ", ""))
user_dic = json.loads(json_str)
data = user_dic["data"]
print(data is None)
if data is not None:
follow = data["total"]
print("关注:", follow)
if follow != 0:
for user in data["list"]:
time.sleep(0.5)
username = user["username"]
nickname = user["nickname"]
blogUrl = user["blogUrl"]
years = user["years"]
print("插入数据", nickname)
list = self.help.select_data_user(nickname)
if len(list) == 0: # 判断数据库中有没有这个名字
self.help.insert_data_user(username, nickname, blogUrl, years, follow)
self.post_url(nickname)
self.parse_html(nickname)
else:
print("已经插入过:", nickname)
def get_url(self, name):
print("获取网址:", self.url + name)
respon = self.seesion.get(url=self.url + name, headers=self.head, cookies=self.cookices)
# print("请求网址响应:" + respon.content.decode('utf-8'))
return respon.content.decode('utf-8')
def post_url(self, name):
data = {
"username": name
}
response = request.post('https://me.youkuaiyun.com/api/relation/create?source=blog', data=json.dumps(data),
cookies=self.cookices)
if response.status_code == 200:
print("关注成功")
else:
print("关注失败状态码:", response.status_code)
def run(self):
self.parse_html("qq_41329275")
#连接MySql数据库
coon = pymysql.connect(
host="127.0.0.1",
# 你的主机
user="root",
# 你的数据库用户名
password="root",
# 本次操作所要使用的数据库
database="spiderdb",
charset="utf8",
)
# 获取数据库操作游标
cursor = coon.cursor()
class MysqlHelp(object):
#插入数据
def insert_data_user(self, username, nickname, userAvatar, years, follow):
global cursor, coon
insertSql = 'insert into csdn_user(username,nickname,blogUrl,years,follow) value (%s,%s,%s,%s,%s)'
cursor.execute(insertSql, (username, nickname, userAvatar, years, follow))
# self.cursor.executemany(sql,data) #批量添加数据
coon.commit()
lastId = cursor.lastrowid
print("lastId", lastId)
def select_data_user(self, nickname):
global cursor, coon
selectSql = 'select nickname from csdn_user where nickname=' + "'" + nickname + "'"
cursor.execute(selectSql)
list = cursor.fetchall()
print("数据库种nickName:", list)
return list
# 关闭数据库
def close_db(self):
cursor.close()
coon.close()
if __name__ == '__main__':
spider = CsdnSpider()
spider.run()
用户名和token查看,登录成功找个接口查看。
三、总结
- 学习安装Mysql。
- sql语句复习。
- 接口参数逻辑解析,发送json数据。
- seesion登录信息保存。