├── Crawler_source_code_result ├── collectWeiboDataByKeyword.py ├── login.py ├── mymain.py └── weiboData.xls ├── Data_analysis_results ├── Black_gold_card.png ├── Code_Payment.xls ├── Code_payment.png ├── Creditcard.png ├── Phone_bank.png ├── Profitability.png ├── black_gold_card.xls ├── creditcard.xls ├── phonebank.xls ├── server.png ├── server.xls └── 新建 Microsoft Word 文档.docx ├── Data_analysis_source_code ├── .gitignore ├── Code_Payment.py ├── README.md ├── analysisplot.py ├── black_gold_card.py ├── creditcard.py ├── demo.py ├── phonebank.py ├── qcloudapi-sdk-python-master │ ├── .gitignore │ ├── README.md │ ├── demo.py │ └── src │ │ ├── QcloudApi │ │ ├── __init__.py │ │ ├── common │ │ │ ├── __init__.py │ │ │ ├── request.py │ │ │ └── sign.py │ │ ├── modules │ │ │ ├── __init__.py │ │ │ ├── account.py │ │ │ ├── base.py │ │ │ ├── bill.py │ │ │ ├── bm.py │ │ │ ├── cbs.py │ │ │ ├── cdb.py │ │ │ ├── cdn.py │ │ │ ├── cmem.py │ │ │ ├── cvm.py │ │ │ ├── dfw.py │ │ │ ├── eip.py │ │ │ ├── image.py │ │ │ ├── lb.py │ │ │ ├── live.py │ │ │ ├── market.py │ │ │ ├── monitor.py │ │ │ ├── redis.py │ │ │ ├── scaling.py │ │ │ ├── sec.py │ │ │ ├── snapshot.py │ │ │ ├── tdsql.py │ │ │ ├── trade.py │ │ │ ├── vod.py │ │ │ ├── vpc.py │ │ │ ├── wenzhi.py │ │ │ └── yunsou.py │ │ └── qcloudapi.py │ │ └── __init__.py ├── serveranalysis.py └── src │ ├── QcloudApi │ ├── __init__.py │ ├── common │ │ ├── __init__.py │ │ ├── request.py │ │ └── sign.py │ ├── modules │ │ ├── __init__.py │ │ ├── account.py │ │ ├── base.py │ │ ├── bill.py │ │ ├── bm.py │ │ ├── cbs.py │ │ ├── cdb.py │ │ ├── cdn.py │ │ ├── cmem.py │ │ ├── cvm.py │ │ ├── dfw.py │ │ ├── eip.py │ │ ├── image.py │ │ ├── lb.py │ │ ├── live.py │ │ ├── market.py │ │ ├── monitor.py │ │ ├── redis.py │ │ ├── scaling.py │ │ ├── sec.py │ │ ├── snapshot.py │ │ ├── tdsql.py │ │ ├── trade.py │ │ ├── vod.py │ │ ├── vpc.py │ │ ├── wenzhi.py │ │ └── yunsou.py │ └── qcloudapi.py │ └── __init__.py ├── README.md ├── 基于微博爬虫的舆情分析_李玉珍.pdf └── 基于微博爬虫的舆情分析_李玉珍.pptx /Crawler_source_code_result/collectWeiboDataByKeyword.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Zhang Huajian' 4 | 5 | ''' 6 | 以关键词收集新浪微博 7 | ''' 8 | import wx 9 | import sys 10 | import urllib 11 | import urllib2 12 | import re 13 | import json 14 | import hashlib 15 | import os 16 | import time 17 | from datetime import datetime 18 | from datetime import timedelta 19 | import random 20 | from lxml import etree 21 | import logging 22 | import xlwt 23 | import xlrd 24 | from xlutils.copy import copy 25 | from datetime import datetime 26 | 27 | 28 | class CollectData(): 29 | """数据收集类 30 | 利用微博高级搜索功能,按关键字搜集一定时间范围内的微博。 31 | """ 32 | def __init__(self, keyword, startTime, interval='50', flag=True, begin_url_per = "http://s.weibo.com/weibo/"): 33 | self.begin_url_per = begin_url_per #设置固定地址部分,默认为"http://s.weibo.com/weibo/" 34 | self.setKeyword(keyword) #设置关键字 35 | self.setStartTimescope(startTime) #设置搜索的开始时间 36 | #self.setRegion(region) #设置搜索区域 37 | self.setInterval(interval) #设置邻近网页请求之间的基础时间间隔(注意:过于频繁会被认为是机器人) 38 | self.setFlag(flag) #设置 39 | self.logger = logging.getLogger('main.CollectData') #初始化日志 40 | 41 | ##设置关键字 42 | ##关键字需解码 43 | def setKeyword(self, keyword): 44 | self.keyword = keyword.decode('GBK').encode("utf-8") #先将其GBK解码,然后再UTF-8编码,然后再输出: 45 | print 'twice encode:',self.getKeyWord() 46 | 47 | ##设置起始范围,间隔为1天 48 | ##格式为:yyyy-mm-dd 49 | def setStartTimescope(self, startTime): 50 | if not (startTime == '-'): 51 | self.timescope = startTime + ":" + startTime 52 | else: 53 | self.timescope = '-' 54 | 55 | ##设置搜索地区 56 | #def setRegion(self, region): 57 | # self.region = region 58 | 59 | ##设置邻近网页请求之间的基础时间间隔 60 | def setInterval(self, interval): 61 | self.interval = int(interval) 62 | 63 | ##设置是否被认为机器人的标志。若为False,需要进入页面,手动输入验证码 64 | def setFlag(self, flag): 65 | self.flag = flag 66 | 67 | ##构建URL 68 | def getURL(self): 69 | return self.begin_url_per+self.getKeyWord()+"&typeall=1&suball=1×cope=custom:"+self.timescope+"&page=" 70 | ##固定地址+关键字二次UTF-8编码+ 71 | ##http://s.weibo.com/weibo/%25E8%2583%2596%25E7%25BA%25B8%25E5%2592%258C%25E7%2598%25A6%25E7%25BA%25B8%25E7 72 | ##%259A%2584%25E5%258C%25BA%25E5%2588%25AB&typeall=1&suball=1×cope=custom:2017-05-01-0:2017-05-02-0&Refer=g 73 | ##关键字需要进行两次urlencode 74 | def getKeyWord(self): 75 | once = urllib.urlencode({"kw":self.keyword})[3:] #首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%。 76 | return urllib.urlencode({"kw":once})[3:] 77 | 78 | ##爬取一次请求中的所有网页,最多返回50页 79 | def download(self, url, maxTryNum=4): 80 | hasMore = True #某次请求可能少于50页,设置标记,判断是否还有下一页 81 | isCaught = False #某次请求被认为是机器人,设置标记,判断是否被抓住。抓住后,需要复制log中的文件,进入页面,输入验证码 82 | name_filter = set([]) #过滤重复的微博ID set 一个无序不重复元素集 83 | 84 | i = 1 #记录本次请求所返回的页数 85 | while hasMore and i < 51 and (not isCaught): #最多返回50页,对每页进行解析,并写入结果文件 86 | source_url = url + str(i) #构建某页的URL 在原来的基础上加上page后面的页码 87 | data = '' #存储该页的网页数据 88 | goon = True #网络中断标记 89 | ##网络不好的情况,试着尝试请求三次 90 | for tryNum in range(maxTryNum): ##0-3 91 | try: 92 | html = urllib2.urlopen(source_url, timeout=12) 93 | data = html.read() 94 | break 95 | except: 96 | if tryNum < (maxTryNum-1): 97 | time.sleep(10) 98 | else: 99 | print 'Internet Connect Error!' 100 | self.logger.error('Internet Connect Error!') 101 | self.logger.info('url: ' + source_url) 102 | self.logger.info('fileNum: ' + str(fileNum)) 103 | self.logger.info('page: ' + str(i)) 104 | self.flag = False 105 | goon = False 106 | break 107 | if goon: 108 | lines = data.splitlines() ##按照行分隔,返回一个包含各行作为元素的列表 109 | isCaught = True 110 | for line in lines: 111 | ## 判断是否有微博内容,出现这一行,则说明没有被认为是机器人 112 | if line.startswith('