RJ博客

Python多线程 - 刷简书阅读数

本文目录

今天发现简书在未登录状态下,访问一次阅读数+1,基于这个逻辑想到了再Linux下用curl访问链接增加阅读数,发现curl访问并没有使阅读数增加,基本确定简书是用ajax请求接口使阅读数+1的,经过排查发现是 mark_viewed.json 这个接口,下面以《QQ麻将番数规则》这篇文章为例讲解下,阅读数上报接口:

http://www.jianshu.com/notes/1a7358890b15/mark_viewed.json

一个简单的版本:

import requests
import thread

headers = {'Referer': 'http://www.jianshu.com/p/1a7358890b15'}
user_data = {'uuid':'b3a9e7fd-f0e1-40ce-adb9-d0cb61796880'}
url = 'http://www.jianshu.com/notes/1a7358890b15/mark_viewed.json'

data = []
count = 0

while 1:
    response = requests.post(url, headers=headers, data=user_data)
    
    count+=1
    if count%10==0:
        print count

运行结果:

C:\Users\Administrator\Desktop>python read_count.py
10
20
30
40
50

已经能让阅读数增加了,但是速度比较慢,而且此版本网络不好的话,post会报错,导致所属进程终止运行,当所有进程都被终止了,程序也就不会生效了。

于是添加了抛异常模块,同时于是结合了python的多线程编程:

import requests
import thread
import time

# header
headers = {'Referer': 'http://www.jianshu.com/p/1a7358890b15'}
# user_data
user_data = {'uuid':'b3a9e7fd-f0e1-40ce-adb9-d0cb61796880'}
# url
url = 'http://www.jianshu.com/notes/1a7358890b15/mark_viewed.json'

data = []
count = 0
passes = 0 

def calculate():
    global count,passes
    while 1:
        '''抛异常,不然网络不好的话,post会报错,导致所属进程终止运行,
        当所有进程都被终止了,程序也就不会生效了'''
        try:
            response = requests.post(url, headers=headers, data=user_data)
        except Exception as e:
            passes+=1
            print 'pass:'+str(passes)
        
        count+=1
        if count%100==0:
            print count

def runThread():
    for x in xrange(1,50):
        thread.start_new_thread( calculate, () )

if __name__ == '__main__':
    # 创建新线程
    try:
        runThread()
    except:
        print "Error: unable to start thread"

    while 1:
        time.sleep(1)
        pass

另一种多线程写法:

import requests
import threading
import time
 
# header
headers = {'Referer': 'http://www.jianshu.com/p/1a7358890b15'}
# user_data
user_data = {'uuid':'b3a9e7fd-f0e1-40ce-adb9-d0cb61796880'}
# url
url = 'http://www.jianshu.com/notes/1a7358890b15/mark_viewed.json'

data = []
threads = []
count = 0
passes = 0

class myThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        # print "Starting " + self.name
        print_time(self.name, self.counter, 100)

def print_time(threadName, delay, counter):
    global count,passes
    while counter:
        # time.sleep(delay)
        try:
            response = requests.post(url, headers=headers, data=user_data)
        except Exception as e:
            passes+=1
            print 'pass:'+str(passes)

        count+=1
        if count%100==0:
            print count

        counter -= 1

def runThread():
    for x in xrange(1,500):
        # 创建新线程
        exec("thread%s = myThread(%s, 'Thread-%s', 1)" %(x,x,x))
        # 开启新线程
        exec("thread%s.start()" %(x))
        # 添加线程到线程列表
        exec("threads.append(thread%s)" %(x))
    
 
count = 0
while count>=0:
    t0 = time.time()

    # 等待所有线程完成
    for t in threads:
        t.join()

    print "Exiting Main Thread"
    t1 = time.time()
    print "time used:"+str(t1-t0)

    print "Starting Main Thread"
    print "Total:"+str(count*50000)+"\n"
    count += 1

    runThread()

Windows下测试,刷50000次耗时127秒左右:

Exiting Main Thread
time used:127s
Starting Main Thread
Total:50000


References:

http://www.runoob.com/python/python-multithreading.html

相关推荐

发表评论

  • 欢迎评论

  • Thanks for stopping by my page! I’m Kush Oneill. Even though I jokingly credit my grandmother for my writing talent, I know that it is a ability I have fostered from childhood. Though my aunt is a writer, I also started out young. I’ve always had a way with words, according to my favorite professor . I was always so excited in English when we had to do a research paper . Now, I help current students achieve the grades that have always come easily to me. It is my way of giving back to students because I understand the troubles they must overcome to graduate. Kush – Academic Writing Professional – <http>//semaacademy.com/]Semaacademy Band

  • INIMITABLE LOLITA CLIPS : sss.unoforum.pro/?0-0 vk.io/gUcgtXM utka.su/KMRTJ l2u.su/qblD

  • KINDEST LOLITA CLIPS : sss.unoforum.pro/?0-0 vk.io/gUcgtXM utka.su/KMRTJ l2u.su/qblD

  • Софосбувир - препарат для лечения хронического гепатита С - основа большинства схем терапии

  • Гражданство Израиля женщинам СНГ. Брак реальный или Деловой. Обращаться на почту znakfortune@gmail.com

  • A-ONE LOLITA CLIPS : sss.unoforum.pro/?0-0 vk.io/gUcgtXM utka.su/KMRTJ l2u.su/qblD

  • With the use of the long ball, the Red Sox conquered the Los Angeles Dodgers 5-1 on Sunday at Dodger Stadium, winning the 114th edition of the Fall Common four games to one. Charlie Pearce hit two house runs for Boston, joining Babe Ruth and Jim Kluszewski as the only players 35 or older to have a multi-homer World Series game. The first was a two-run shot in the top of the first of all inning. He later had a solo home run in the eighth. Pearce, a journeyman who has played for every staff in the American League East division, had three residence runs and eight RBI in five games. He was named World Series MVP. "Baseball is a funny game, " Pearce said to Monk Sports. "The longer you stay in the game, great issues can happen. I'm very blessed to be here right now. inches Mookie Betts and M. D. Martinez added single home runs in the sixth and seventh, respectively. Clayton Kershaw, who may have played his final game in a Dodgers uniform, took the loss.

  • CLOISTERED LOLITA CLIPS : sss.unoforum.pro/?0-0 vk.io/gUcgtXM utka.su/KMRTJ l2u.su/qblD

  • Очень любопытный вопрос

  • UNRIVALLED LOLITA CLIPS : sss.unoforum.pro/?0-0 vk.io/gUcgtXM utka.su/KMRTJ l2u.su/qblD

  • Hello, I’m Marnie Dalby. Welcome to my website . I started writing in high school after a creative writing assignment for my English teacher. I did creative writing for almost a year before I thought about doing something else. I had always loved doing research papers because I’m passionate about learning. When you combine writing skill with a love of learning, dissertation writing only makes sense as a job. I’m passionate about assisting the students of the future in their school career. When they get too busy, I am there to help. Marnie Dalby – Writing Expert - <http>//www.peacerivermuseum.com/]Peacerivermuseum Team

  • Hi there, I enjoy reading through your article. I wanted to write a little comment to support you.

  • damei yamiedie