该代码也是失败的,失败的原因大致有两个,一个是百度有一个bsk参数,我不知道他是如何产生的,通过调用web开发者,我对网页做了定点调试,但是发现之后会陷入一个无限循环的巨大矩阵中,我的web调试能力太弱,对于很多隐藏的功能还不会使用导致我无法找出bsk实现的算法。第二我看不懂js语言,这让我无法使用python语言来模拟js的算法实现bsk参数。所以最后我是通过网上的高手写的一串js代码,调用火狐浏览器运行这段js代码然后生成一个bsk参数而来的。
最后我做到了回复文字以及图片的功能,但是遇到了另一个难题,一个账号发了7-8贴以后就被封掉了,至于原因应该还是参数或者ip问题或者账号本身的异地登录安全限制问题。我没有细究下去。因为一旦被封号以后又要花钱买账号,账号买好以后有些登录的时候还要你邮箱验证,安全验证的情况太多,导致我最后没有在写下去。整个模块都是多线程状态运行的,所以性能方面很好,快速高效,但是因为调用火狐会花去很长时间,导致效率变低。
# -*- coding:utf-8 -*-
# 貼吧1.15
import os
import queue
import threading
import time
from threading import Timer
from net_work.attention_bar import attention_barname, reject_tie, fa_tie
from net_work.default_landing import defacult_account
COOKIES_POOL = 'E:\百度贴吧\cookies_pool'
BA_FILE = 'E:\百度贴吧\吧.txt'
class Main_tieba(object):
name = '百度发帖'
def __init__(self, ns):
self.ns = ns # 任務數
self.n = 15 # 引流的條數
self.count = threading.local()
self.count.orignal = 0 # 成功發帖的個數
def run(self, kw):
'''运行任务'''
q = queue.Queue(self.n)
tid = threading.Thread(target=self.get_tid, args=(kw, q, self.n - self.count.orignal))
tid.start()
threads = []
while 1:
cookie = self.read_cookies()
if cookie:
for i in threads:
if not i.is_alive():
threads.remove(i)
if len(threads) <10: # 每次并發這去執行3次
attention = threading.Thread(target=self.get_attention, args=(kw, cookie))
attention.start()
tid = q.get(block=True, timeout=10)
reply_tie = threading.Thread(target=self.reply_tie, args=(tid, cookie))
reply_tie.start()
threads.append(reply_tie)
if self.ns is 0:
return
elif self.count.orignal is self.n:
self.ns -= 1
yield kw
else:
time.sleep(0.5)
def get_tid(self, kw, q, n):
'''獲取tid'''
while 1:
while q.qsize() < 3:
ids = iter(reject_tie(kw, n))
while not q.full():
try:
q.put(next(ids))
except StopIteration as e:
break
else:
time.sleep(5)
def reply_tie(self, task, cookie):
'''回帖功能'''
while 1:
if fa_tie(task, cookie):
self.count.orignal += 1
break
def get_attention(self, kw, cookie):
'''關注貼吧'''
attention_barname(kw, cookie)
def read_cookies(self, ):
'''讀取cookies'''
cookie = set(os.listdir(COOKIES_POOL))
cookie = iter(cookie)
try:
return next(cookie)
except StopIteration as e:
return False
def get_cookies():
'''獲取cookies'''
if len(os.listdir(COOKIES_POOL)) < 4 and c.empty():
default_ = threading.Thread(target=defacult_account, args=(4, c))
default_.start()
default_.join()
global timer
timer = Timer(0.5, get_cookies)
timer.start()
c = queue.Queue(maxsize=4)
event = threading.Event()
timer = Timer(0.1, get_cookies)
timer.start()
with open(file=BA_FILE, encoding='utf-8', mode='r') as file:
kw_list = file.read().splitlines()
kw_ = iter(kw_list)
ns = len(kw_list)
if __name__ == "__main__":
while True:
try:
kw = next(kw_)
spider = Main_tieba(ns, ).run(kw)
next(spider)
except StopIteration as e:
break