juanboy
  1 +# Default ignored files
  2 +/shelf/
  3 +/workspace.xml
  4 +# Editor-based HTTP Client requests
  5 +/httpRequests/
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<module type="PYTHON_MODULE" version="4">
  3 + <component name="Flask">
  4 + <option name="enabled" value="true" />
  5 + </component>
  6 + <component name="NewModuleRootManager">
  7 + <content url="file://$MODULE_DIR$" />
  8 + <orderEntry type="inheritedJdk" />
  9 + <orderEntry type="sourceFolder" forTests="false" />
  10 + </component>
  11 + <component name="TemplatesService">
  12 + <option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
  13 + <option name="TEMPLATE_FOLDERS">
  14 + <list>
  15 + <option value="$MODULE_DIR$/templates" />
  16 + </list>
  17 + </option>
  18 + </component>
  19 +</module>
  1 +<component name="InspectionProjectProfileManager">
  2 + <profile version="1.0">
  3 + <option name="myName" value="Project Default" />
  4 + <inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
  5 + <option name="ignoredErrors">
  6 + <list>
  7 + <option value="N802" />
  8 + </list>
  9 + </option>
  10 + </inspection_tool>
  11 + <inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
  12 + <option name="processCode" value="true" />
  13 + <option name="processLiterals" value="true" />
  14 + <option name="processComments" value="true" />
  15 + </inspection_tool>
  16 + </profile>
  17 +</component>
  1 +<component name="InspectionProjectProfileManager">
  2 + <settings>
  3 + <option name="USE_PROJECT_PROFILE" value="false" />
  4 + <version value="1.0" />
  5 + </settings>
  6 +</component>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="Black">
  4 + <option name="sdkName" value="Python 3.12 (Desktop)" />
  5 + </component>
  6 + <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (Desktop)" project-jdk-type="Python SDK" />
  7 +</project>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="ProjectModuleManager">
  4 + <modules>
  5 + <module fileurl="file://$PROJECT_DIR$/.idea/Weibo_PublicOpinion_AnalysisSystem.iml" filepath="$PROJECT_DIR$/.idea/Weibo_PublicOpinion_AnalysisSystem.iml" />
  6 + </modules>
  7 + </component>
  8 +</project>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="VcsDirectoryMappings">
  4 + <mapping directory="" vcs="Git" />
  5 + </component>
  6 +</project>
  1 +import time
  2 +import requests
  3 +import csv
  4 +import os
  5 +from datetime import datetime
  6 +
  7 +def init():
  8 + if not os.path.exists('./articleData.csv'):
  9 + with open('./articleData.csv','w',encoding='utf-8',newline='') as csvFile:
  10 + writer = csv.writer(csvFile)
  11 + writer.writerow([
  12 + 'id',
  13 + 'likeNum',
  14 + 'commentsLen',
  15 + 'reposts_count',
  16 + 'region',
  17 + 'content',
  18 + 'contentLen',
  19 + 'created_at',
  20 + 'type',
  21 + 'detailUrl',# followBtnCode>uid + mblogid
  22 + 'authorAvatar',
  23 + 'authorName',
  24 + 'authorDetail',
  25 + 'isVip' # v_plus
  26 + ])
  27 +
  28 +def writerRow(row):
  29 + with open('./articleData.csv', 'a', encoding='utf-8', newline='') as csvFile:
  30 + writer = csv.writer(csvFile)
  31 + writer.writerow(row)
  32 +
  33 +def get_data(url,params):
  34 + headers = {
  35 + 'Cookie':'SINAGLOBAL=2555941826014.1074.1676801766625; ULV=1719829459275:6:1:2:4660996305989.918.1719827559898:1719743122299; UOR=,,www.baidu.com; XSRF-TOKEN=VtLXviYSIs8lor7sz4iGyigL; SUB=_2A25LhvU9DeRhGeFH6FIX-S3MyD2IHXVo-gj1rDV8PUJbkNAGLRXMkW1Ne2nhI3Gle25QJK0Z99J3trq_NZn6YKJ-; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WW3Mv8V5EupQbbKh.vaZIwU5JpX5KzhUgL.FoM4e05c1Ke7e022dJLoIp7LxKML1KBLBKnLxKqL1hnLBoM41hz41hqReKqN; WBPSESS=Dt2hbAUaXfkVprjyrAZT_LRaDLsnxG-kIbeYwnBb5OUKZiwfVr_UrcYfWuqG-4ZVDM5HeU3HXkDNK_thfRfdS9Ao6ezT30jDksv-CpaVmlTAqGUHjJ7PYkH5aCK4HLxmRq14ZalmQNwzfWMPa4y0VNRLuYdg7L1s49ymNq_5v5vusoz0r4ki6u-MHGraF0fbUTgX14x0kHayEwOoxfLI-w==; SCF=AqmJWo31oFV5itnRgWNU1-wHQTL6PmkBLf3gDuqpdqAIfaWguDTMre6Oxjf5Uzs74JAh2r0DdV1sJ1g6m-wJ5NQ.; _s_tentry=-; Apache=4660996305989.918.1719827559898; PC_TOKEN=7955a7ab1f; appkey=; geetest_token=602cd4e3a7ed1898808f8adfe1a2048b; ALF=1722421868',
  36 + 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'
  37 + }
  38 + response = requests.get(url,headers=headers,params=params)
  39 + if response.status_code == 200:
  40 + return response.json()['statuses']
  41 + else:
  42 + return None
  43 +
  44 +def getAllTypeList():
  45 + typeList = []
  46 + with open('./navData.csv','r',encoding='utf-8') as reader:
  47 + readerCsv = csv.reader(reader)
  48 + next(reader)
  49 + for nav in readerCsv:
  50 + typeList.append(nav)
  51 + return typeList
  52 +
  53 +def parse_json(response,type):
  54 + for artice in response:
  55 + id = artice['id']
  56 + likeNum = artice['attitudes_count']
  57 + commentsLen = artice['comments_count']
  58 + reposts_count = artice['reposts_count']
  59 + try:
  60 + region = artice['region_name'].replace('发布于 ', '')
  61 + except:
  62 + region = '无'
  63 + content = artice['text_raw']
  64 + contentLen = artice['textLength']
  65 + created_at = datetime.strptime(artice['created_at'],'%a %b %d %H:%M:%S %z %Y').strftime('%Y-%m-%d')
  66 + type = type
  67 + try:
  68 + detailUrl = 'https://weibo.com/' + str(artice['id']) + '/' + str(artice['mblogid'])
  69 + except:
  70 + detailUrl = '无'
  71 + authorAvatar = artice['user']['avatar_large']
  72 + authorName = artice['user']['screen_name']
  73 + authorDetail = 'https://weibo.com/u/' + str(artice['user']['id'])
  74 + isVip = artice['user']['v_plus']
  75 + writerRow([
  76 + id,
  77 + likeNum,
  78 + commentsLen,
  79 + reposts_count,
  80 + region,
  81 + content,
  82 + contentLen,
  83 + created_at,
  84 + type,
  85 + detailUrl,
  86 + authorAvatar,
  87 + authorName,
  88 + authorDetail,
  89 + isVip
  90 + ])
  91 +
  92 +def start(typeNum=1,pageNum=1):
  93 + articleUrl = 'https://weibo.com/ajax/feed/hottimeline'
  94 + init()
  95 + typeList = getAllTypeList()
  96 + typeNumCount = 0
  97 + for type in typeList:
  98 + if typeNumCount > typeNum:return
  99 + time.sleep(2)
  100 + for page in range(0,pageNum):
  101 + print('正在爬取的类型:%s 中的第%s页文章数据' % (type[0],page + 1))
  102 + time.sleep(1)
  103 + parmas = {
  104 + 'group_id':type[1],
  105 + 'containerid':type[2],
  106 + 'max_id':page,
  107 + 'count':10,
  108 + 'extparam':'discover|new_feed'
  109 + }
  110 + response = get_data(articleUrl,parmas)
  111 + parse_json(response,type[0])
  112 + typeNumCount += 1
  113 +
  114 +if __name__ == '__main__':
  115 + start()
  116 +
  117 +
  118 +
  119 +
  120 +
  121 +
  122 +
  123 +
  1 +import requests
  2 +import csv
  3 +import numpy as np
  4 +import os
  5 +
  6 +def init():
  7 + if not os.path.exists('./navData.csv'):
  8 + with open('./navData.csv','w',encoding='utf-8',newline='') as csvFile:
  9 + writer = csv.writer(csvFile)
  10 + writer.writerow([
  11 + 'typeName',
  12 + 'gid',
  13 + 'containerid'
  14 + ])
  15 +
  16 +def writerRow(row):
  17 + with open('./navData.csv', 'a', encoding='utf-8', newline='') as csvFile:
  18 + writer = csv.writer(csvFile)
  19 + writer.writerow(row)
  20 +
  21 +def get_data(url):
  22 + headers = {
  23 + 'Cookie':'SINAGLOBAL=2555941826014.1074.1676801766625; ULV=1719829459275:6:1:2:4660996305989.918.1719827559898:1719743122299; UOR=,,www.baidu.com; XSRF-TOKEN=VtLXviYSIs8lor7sz4iGyigL; SUB=_2A25LhvU9DeRhGeFH6FIX-S3MyD2IHXVo-gj1rDV8PUJbkNAGLRXMkW1Ne2nhI3Gle25QJK0Z99J3trq_NZn6YKJ-; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WW3Mv8V5EupQbbKh.vaZIwU5JpX5KzhUgL.FoM4e05c1Ke7e022dJLoIp7LxKML1KBLBKnLxKqL1hnLBoM41hz41hqReKqN; WBPSESS=Dt2hbAUaXfkVprjyrAZT_LRaDLsnxG-kIbeYwnBb5OUKZiwfVr_UrcYfWuqG-4ZVDM5HeU3HXkDNK_thfRfdS9Ao6ezT30jDksv-CpaVmlTAqGUHjJ7PYkH5aCK4HLxmRq14ZalmQNwzfWMPa4y0VNRLuYdg7L1s49ymNq_5v5vusoz0r4ki6u-MHGraF0fbUTgX14x0kHayEwOoxfLI-w==; SCF=AqmJWo31oFV5itnRgWNU1-wHQTL6PmkBLf3gDuqpdqAIfaWguDTMre6Oxjf5Uzs74JAh2r0DdV1sJ1g6m-wJ5NQ.; _s_tentry=-; Apache=4660996305989.918.1719827559898; PC_TOKEN=7955a7ab1f; appkey=; geetest_token=602cd4e3a7ed1898808f8adfe1a2048b; ALF=1722421868',
  24 + 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'
  25 + }
  26 + params = {
  27 + 'is_new_segment':1,
  28 + 'fetch_hot':1
  29 + }
  30 + response = requests.get(url,headers=headers,params=params)
  31 + if response.status_code == 200:
  32 + return response.json()
  33 + else:
  34 + return None
  35 +
  36 +def parse_json(response):
  37 + navList = np.append(response['groups'][3]['group'],response['groups'][4]['group'])
  38 + for nav in navList:
  39 + navName = nav['title']
  40 + gid = nav['gid']
  41 + containerid = nav['containerid']
  42 + writerRow([
  43 + navName,
  44 + gid,
  45 + containerid
  46 + ])
  47 +
  48 +
  49 +if __name__ == '__main__':
  50 + init()
  51 + url = 'https://weibo.com/ajax/feed/allGroups'
  52 + response = get_data(url)
  53 + parse_json(response)