Python微博项目

  • 功能
    • 批量获取微博博文mid(每篇博文唯一id)并插入数据库
    • 批量登录微博账号(无需验证码,为什么我也不知道,一个账号需要.4个账号就直接上去了)并插入数据库
    • 批量发表随机微博
    • 批量转发微博(开发中)
    • 批量关注微博(开发中)
    • 批量发私信(开发中)
    • 批量发评论(已完成)
    • 多线程(已完成80%)
    • 代理请求(开发中)
  • 目录结构
    • WeiBo
      • Mode
        • __init__.py
        • Conn.py
        • Get_Data.py
        • Get_Mid.py
        • Login_Session.py
        • Post.py
        • Send_Content.py
        • User_Agent.py
        • data
          • account.config
          • content.config
          • cookie.config
          • send_content.config
          • user_agent.config
      • Main.py
  • 数据库结构
    • autodrain(数据库)
      • cookies
        • id(id)
        • cookies(cookies)
      • proxy
        • id
        • addres(ip地址)
        • port(端口)
        • anonymous(是否匿名)
        • types(代理类型)
        • position(位置)
        • delay(延迟)
        • last_test(最后测试时间)
      • user
        • id(单条信息id,主键自增)
        • mid(博文id)
        • source(发表使用的手机型号)
        • text(博文内容)
        • comments_count(忘了)
        • reposts_count(转发数量)
        • avatar_hd(高清头像url)
        • description(个人简介)
        • mbrank(会员等级)
        • urank(账号等级)
        • verified_reason(认证说明)
        • follow_count(关注人数)
        • followers_count(粉丝数量)
        • gender(性别m男f女)
        • screen_name(昵称)
        • statuses_count(发表文章数量)
        • profile_url(用户主页url)
      • temp_mid
        • id(id)
        • mid(博文mid)

 

 

 

 

# Main.py

# _*_ coding: utf-8 _*_
import sys
import time
import string
import threading
import datetime
import sys
sys.path.append("../../")
from model.WeiBo.Mode import Get_Data,Post,Login_Session,Send_Content


def Main():
    Post.Go()

def Send():
    Send_Content.Start()



if __name__ == '__main__':
    # Cookie()
    Main()
    # Send()
    # mid = "4388669999421483"
    # Post.Start(Login_Session.Main(Get_Data.Account()),mid)
# Conn.py

# -*- coding: UTF-8 -*-
import types
import pymysql
import pymysql.cursors


# def Main(mid,source,text,user_id,user_avatar_hd,user_badge_bind_taobao,user_badge_user_name_certificate,user_description,user_mbrank,user_urank,user_profile_url,user_verified_reason,user_follow_count,user_followers_count,user_gender,user_screen_name,user_statuses_count):
#     # 打开数据库连接
#     db = pymysql.connect("localhost", "root", "root", "autodrain", charset = 'utf8')
#     # 使用cursor()方法获取操作游标
#     cursor = db.cursor()
#     cursor.execute("SELECT VERSION()")
#     sql = """INSERT INTO mblog_user ('mid', 'source','source','text','user_id','user_avatar_hd','user_badge_bind_taobao','user_badge_user_name_certificate','user_description','user_mbrank','user_urank','user_profile_url','user_verified_reason','user_follow_count','user_followers_count','user_gender','user_screen_name','user_statuses_count') VALUES('test1', 1),('test2', 2),('test3', 3),('test4', 4),('test5', 5),('test6', 6);"""
#     try:
#         # 获取一个游标
#         with db.cursor() as cursor:
#             sql = 'select * from mblog_user'
#             cout = cursor.execute(sql)
#             print("数量: " + str(cout))
#             db.commit()
#
#     finally:
#         db.close()

def GetConn():
    try:
        db = pymysql.connect("127.0.0.1", "root", "root", "autodrain", charset = 'utf8')
        return db
    except:
        print("数据库连接失败,请检查")



#查询数据并返回结果集
def Find_All():
    # 打开数据库连接
    db = GetConn()
    # 使用cursor()方法获取操作游标
    try:
        # 获取一个游标
        with db.cursor() as cursor:
            sql = 'select * from user'
            cursor.execute(sql)
            results = cursor.fetchall()
            for row in results:
                mid = row[3]
                print(mid)
            db.commit()
            return results
    finally:
        db.close()





# def Inter_Data=""
    # def Main(mid,source,text,user_id,user_avatar_hd,user_badge_bind_taobao,user_badge_user_name_certificate,user_description,user_mbrank,user_urank,user_profile_url,user_verified_reason,user_follow_count,user_followers_count,user_gender,user_screen_name,user_statuses_count):


# Post.py
# _*_ coding: utf-8 _*_
import requests
import threading
import random
import time
import re
import sys
sys.path.append("../../../")
from model.WeiBo.Mode import Conn,User_Agent


def Head(cook):
    user_agent = User_Agent.Main()
    headers = {
        "authority": "www.weibo.com",
        "method": "POST",
        "path": "/aj/v6/comment/add?ajwvr=6&__rnd=" + str(int(round(time.time() * 1000))),
        "scheme": "https",
        "accept": "*/*",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh,zh-CN;q=0.9",
        "content-length": "162",
        "content-type": "application/x-www-form-urlencoded",
        "cookie": cook,
        "origin": "https://www.weibo.com",
        "referer": "https://www.weibo.com/u/2527670372?is_hot=1",
        "user-agent": user_agent,
        "x-requested-with": "XMLHttpRequest"
    }
    return headers


def Url_Data():
    url_str1 = "https://www.weibo.com/aj/v6/comment/add?ajwvr=6&__rnd=" + str(int(round(time.time() * 1000)))
    # url_str2= "https://www.weibo.com/aj/v6/comment/add?ajwvr=6&__rnd=1561631552951"
    # url_str3= "https://www.weibo.com/aj/v6/comment/add?ajwvr=6&__rnd=1561631552951"
    # str = [url_str1,url_str2,url_str3]
    return url_str1


def Post_Data(uid, mid, ci):
    str1 = "第" + str(ci) + "次,调试2"
    data = {
        "act": "post",
        "mid": mid,
        "uid": uid,
        "forward": "0",
        "isroot": "0",
        "content": str1,
        "location": "page_103505_home",
        "module": "scommlist",
        "group_source": "",
        "pdetail": "1035052527670372",
        "_t": "0",
    }
    # "pdetail": "1035052527670372",
    # print("UID:",uid)
    return data


'''
函数内参数
Fail_Pan:判断挂了多少次,次数超过5次自动切断程序运行;int类型
Number_For:每个账号循环多少次/提交多少次;int类型


函数传参参数
cook:包含每个用户的cook和uid,list类型
mid:目标博文mid参数;string类型

调用本函数必须要给的参数
cookies:列表类型
pdetail:string类型
mid:string类型
'''




def Get_Mid():
    db = Conn.GetConn()
    res = []
    try:
        # 获取一个游标
        with db.cursor() as cursor:
            sql = 'select mid from user'
            cursor.execute(sql)
            results = cursor.fetchall()
            for row in results:
                mid = row[0]
                res.append(mid)
            db.commit()
    finally:
        db.close()
    return random.choice(res)


#获取cookies
def Cook():
    cookies = []
    T = True
    while T:
        try:
            db = Conn.GetConn()
            cur = db.cursor()
            sql = "select * from cookies"
            cur.execute(sql)
            res = cur.fetchall()
            for row in res:
                cookies.append(row[1])
            db.commit()
            db.close()
            T = False
            return cookies
        except:
            print("                                           ===================获取Cookies错误,正在重新获取===============\n")



def Start(start,stop):
    T = True
    while T:
        try:
            cookies = Cook()
            num = 1
            uid = []
            cook = []
            Fail_Pan = 1
            # Number_For = 20
            url = Url_Data()
            for c in range(0, int(len(cookies))):  # 提取cookies和uid并添加到列表
                uid.append(str(re.findall(r"____________(..........+?)", str(cookies[c]))).replace("['", "").replace("']", ""))
                cook.append(str(re.findall(r"(.+?)____________", str(cookies[c]))).replace("['", "").replace("']", ""))
            for cok in range(0, int(len(cookies))):  # 每个账号的循环
            # for cok in range(start, stop):  # 每个账号的循环
                cook_use = cook[cok]  # 获取单次cookie
                uid_use = uid[cok]  # 获取单次uid
                for x in range(start,stop):  # 循环次数及提交
                    hander = Head(cook_use)  # 获取每次提交的请求头
                    mid = str(Get_Mid())
                    post_data = Post_Data(uid_use, mid, num)  # 获取每次提交的Post内容
                    resp = requests.post(url, post_data, headers = hander, timeout = 5)  # 提交
                    if "100000" in resp.text:
                        print("\n                                           ===================第", str(num), "次刷评论,成功===============\n")
                        num = num + 1
                        continue
                    elif "100001" in resp.text:
                        print("\n                                           ==============第", str(x + 1), "次失败,原因:次数太多=============\n")
                        Fail_Pan = Fail_Pan + 1
                        continue
                    elif "100002" in resp.text:
                        print("\n                                          ========================失败,原因:服务暂停多=======================\n")
                        Fail_Pan = Fail_Pan + 1
                        continue
                    elif "100024" in resp.text:
                        print("\n                                           ============失败,原因:用户请求特殊接口 (%s) 频次超过上限==========\n")
                        Fail_Pan = Fail_Pan + 1
                        continue
                    elif "100012" in resp.text:
                        print("\n                                           ==========================失败,原因:非法请求多   时间:",'\033[1;32m' + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + '\033[0m'"==============\n")
                        time.sleep(2)
                        Fail_Pan = Fail_Pan + 1
                        continue
                if Fail_Pan >= 50:
                    t = random.randint(300, 800)
                    print("")
                    print("\n                                            --------所有账号全部受限,", str(int(t/60)), "分钟后自动重起程序-------\n")
                    print("")
                    print("")
                    time.sleep(t)
                    continue
        except:
            print("\n                                            ------------------------未知错误,重新启动---------------------\n")




#设置开启多少线程,双层循环,最外层控制循环倍数
def Go():
    start = 1
    stop = 0
    for x in range(0,1):
        for t in range(0,30):
           
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值