加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

统计贴吧股票吧某天的回帖量

发布时间:2020-05-24 23:56:15 所属栏目:Python 来源:互联网
导读:统计贴吧股票吧某天的回帖量

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

# encoding:utf-8

import urllib,urllib2
from pprint import pprint
from collections import Counter
from bs4 import BeautifulSoup
import gevent
from gevent import monkey; monkey.patch_all()

'''
sample urls
http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=0     #page 1
http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=50    #     2
http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=100   #     3
http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=150   #     4

http://tieba.baidu.com/f?kw=gupiao&ie=utf-8&pn=50  #
'''
all_date = []

def get_date_of_reply(tieba_name,start_page=1,end_page=1):
#def get_date_of_reply2(start=0,end=100,step=50): # old params
    global all_date
    do_range = range((start_page-1)*50,end_page*50,50)
    for n in do_range:
        print n
        f = urllib.urlopen('http://tieba.baidu.com/f?kw=%s&ie=utf-8&pn=%s' % (tieba_name,n))

        soup = BeautifulSoup(f.read(),'html.parser')
        #<span class="threadlist_reply_date j_reply_data" title="最后回复时间">            9-15</span>
        onepage_reply_spans = soup.find_all("span",class_="j_reply_data")
        #onepage_date_of_reply = [getattr(one_span,'string').strip() for one_span in onepage_reply_spans]
        onepage_date_of_reply = []
        for one_span in onepage_reply_spans:
            strdate = getattr(one_span,'string')
            if strdate:   
                onepage_date_of_reply.append(strdate.strip())
        # 不统计当天的  当天回复是用时间表示的
        if ':' in onepage_date_of_reply[0]:
            for n in onepage_date_of_reply[:]:
                if ':' in n:
                    onepage_date_of_reply.remove(n)

        all_date.extend(onepage_date_of_reply)

    return onepage_date_of_reply


#print len(get_date_of_reply(start_page=1,end_page=10))


def count_date(tieba_name,startpage,endpage,num=10):
    '''
    num 一个线程处理几页
    '''
    page_range = range(startpage,num)
    threads = []
    for page in page_range:
        threads.append(gevent.spawn(get_date_of_reply,tieba_name,page,page+num-1))
    gevent.joinall(threads)
    c = Counter(all_date)
    return c

if __name__ == '__main__':
    #print test()
    print count_date('gupiao',100,300,3)

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读