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

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

发布时间:2020-12-17 17:22:57 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 # encoding:utf-8import urllib,urllib2from pprint import pprintfrom collections import Counterfrom bs4 import BeautifulSoupimport geventfrom

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

# 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)

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

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

    推荐文章
      热点阅读