├── LICENSE ├── README.md ├── main.py └── main3.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Luogu-Ac-Difficulties 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Luogu-Ac-Difficulties 2 | 3 | # `9.20 UPD:由于特殊原因,此代码闭源。` 4 | 5 | 统计你在洛谷通过的题目难度个数,并按照难度顺序输出详细题目信息(入门难度、普及- 只统计个数不输出明细)。 6 | 7 | **请不要滥用爬虫,以免浪费洛谷流量。若被封 IP,后果自负。** 8 | 9 | **请正确选择您 Python 语言版本对应程序运行。Python 2 用户请使用 main.py,Python 3 用户请使用 main3.py** 10 | 11 | ## 依赖的库: 12 | + requests 13 | + bs4 14 | + lxml 15 | 16 | ## 使用前提: 17 | 请确保你安装了以上三个库。安装方法:**配置好环境变量之后**在命令行输入 18 | ```batch 19 | pip install requests 20 | pip install bs4 21 | pip install lxml 22 | ``` 23 | 24 | ## 使用方法:在 cmd 当中运行(不建议使用 IDLE) 25 | ```batch 26 | python main(3).py 27 | ``` 28 | 然后输入要获取对象的洛谷 uid,按下 `Enter` 即可。 29 | 30 | **注意:如果获取对象开启了完全隐私保护,此脚本无法正常工作。** 31 | 32 | ## Badges 33 | 可以配合[该项目](https://github.com/Anguei/Luogu-Difficulties-Badge-Generator)生成 badges 代码 34 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #-*- coding:utf-8 -*- 2 | import requests 3 | from bs4 import BeautifulSoup 4 | import os 5 | import sys, platform 6 | import warnings 7 | warnings.filterwarnings("ignore") 8 | system = platform.system() 9 | 10 | yellow, green, blue, purple, bluedark, gray, difficulties = [], [], [], [], [], [], [] 11 | colors = { '1r': 0, '2o': 0, '3y': 0, '4g': 0, '5b': 0, '6p': 0, '7d': 0, '8g': 0 } 12 | #topics = map(lambda x: x[1: -1], raw_input().split()) 13 | user = raw_input() 14 | urlUser = 'https://www.luogu.org/space/show?uid=' + user 15 | urlBase = 'https://www.luogu.org/problemnew/show/' 16 | headers = { 17 | u'Referer': u'https://www.luogu.org/recordnew/lists?uid=10000', 18 | u'Accept': u'text/html, */*; q=0.01', 19 | u'Accept-Encoding': u'gzip, deflate, br', 20 | u'Accept-Language': u'zh-CN,zh;q=0.9', 21 | u'User-Agent': u'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36' 22 | } 23 | 24 | # 搞搞 UTF-8 25 | reload(sys) 26 | sys.setdefaultencoding('utf-8') 27 | type = sys.getfilesystemencoding() 28 | 29 | f = open("d:\\LgData.txt", "w") # 仅用于统计这次爬虫运行耗费了多少流量 30 | 31 | # 获取个人主页信息 32 | v = requests.get(url = urlUser, headers = headers, verify = False) 33 | soup1 = BeautifulSoup(v.text, 'lxml') 34 | res = soup1.find('div', {'class': 'lg-article am-hide-sm'}) 35 | topics = map(lambda x: x[1: -1], res.text.split()) 36 | 37 | # 第一个元素不是题目 38 | topics.pop(0) 39 | 40 | finished = 0 41 | 42 | for topic in topics: 43 | try: 44 | v = requests.get(url = urlBase + topic, headers = headers, verify = False, timeout = 10) 45 | except: 46 | #v = requests.get(url = urlBase + topic, headers = headers, verify = False, timeout = 10) 47 | #v = requests.get(url = urlBase + topic, headers = headers, verify = False, timeout = 10) 48 | print topic, "Error!" 49 | continue 50 | 51 | #f.write(v.text) 52 | v = v.text#.decode('utf-8').encode(type) 53 | 54 | soup = BeautifulSoup(v, 'lxml') 55 | try: 56 | difficulty = soup.findAll('span', {'class': 'lg-right'})[2].text # 获取难度 57 | difficulties.append(difficulty) 58 | # 不知道为什么,每次尝试用字典都爆异常,所以暴力 if 59 | if difficulty == '普及/提高-': 60 | print topic, "Yellow" 61 | yellow.append(topic) 62 | elif difficulty == '普及+/提高': 63 | print topic, "Green!" 64 | green.append(topic) 65 | elif difficulty == '提高+/省选-': 66 | print topic, "Blue!!" 67 | blue.append(topic) 68 | elif difficulty == '省选/NOI-': 69 | print topic, "Purple!!!" 70 | purple.append(topic) 71 | elif difficulty == 'NOI/NOI+/CTSC': 72 | print topic, "BlueDark!!!!" 73 | bluedark.append(topic) 74 | elif difficulty == '尚无评定': 75 | print topic, "Gray??" 76 | gray.append(topic) 77 | else: 78 | print topic 79 | except: 80 | print topic, "Didn't allowed!" # 无权查看的题目 81 | 82 | finished += 1 83 | if system == "Windows": 84 | os.system('title U' + user + ': ' + str(finished) + '/' + str(len(topics))) 85 | 86 | print('------------------------------------') 87 | 88 | yellowPlus, greenPlus = 0, 0 89 | 90 | difficulties.sort() 91 | for i in difficulties: 92 | #print i#, d[i] 93 | if i == '入门难度': 94 | colors['1r'] += 1 95 | if i == '普及-': 96 | colors['2o'] += 1 97 | if i == '普及/提高-': 98 | colors['3y'] += 1 99 | yellowPlus += 1 100 | if i == '普及+/提高': 101 | colors['4g'] += 1 102 | yellowPlus += 1 103 | greenPlus += 1 104 | if i == '提高+/省选-': 105 | colors['5b'] += 1 106 | yellowPlus += 1 107 | greenPlus += 1 108 | if i == '省选/NOI-': 109 | colors['6p'] += 1 110 | yellowPlus += 1 111 | greenPlus += 1 112 | if i == 'NOI/NOI+/CTSC': 113 | colors['7d'] += 1 114 | yellowPlus += 1 115 | greenPlus += 1 116 | if i == '尚无评定': 117 | colors['8g'] += 1 118 | print('------------------------------------') 119 | 120 | print 'Red: ', colors['1r'] 121 | print 'Orange: ', colors['2o'] 122 | print 'Yellow: ', colors['3y'] 123 | print 'Green: ', colors['4g'] 124 | print 'Blue: ', colors['5b'] 125 | print 'Purple: ', colors['6p'] 126 | print 'Bluedark: ', colors['7d'] 127 | print 'Gray: ', colors['8g'] 128 | 129 | print('------------------------------------') 130 | 131 | print '\n', str(colors['3y']), 'Yellow:' 132 | for i in range(len(yellow)): 133 | print yellow[i], 134 | if (i + 1) % 10 == 0: 135 | print '\n', 136 | print '\n', str(colors['4g']), 'Green:' 137 | for i in range(len(green)): 138 | print green[i], 139 | if (i + 1) % 10 == 0: 140 | print '\n', 141 | print '\n', str(colors['5b']), 'Blue:' 142 | for i in range(len(blue)): 143 | print blue[i], 144 | if (i + 1) % 10 == 0: 145 | print '\n', 146 | print '\n', str(colors['6p']), 'Purple:' 147 | for i in range(len(purple)): 148 | print purple[i], 149 | if (i + 1) % 10 == 0: 150 | print '\n', 151 | print '\n', str(colors['7d']), 'Bluedark:' 152 | for i in range(len(bluedark)): 153 | print bluedark[i], 154 | if (i + 1) % 10 == 0: 155 | print '\n', 156 | print '\n', str(colors['8g']), 'Gray:' 157 | for i in range(len(gray)): 158 | print gray[i], 159 | if (i + 1) % 10 == 0: 160 | print '\n', 161 | print "\n" 162 | 163 | print('------------------------------------') 164 | 165 | print 'Yellow+: ', yellowPlus 166 | print 'Green+: ', greenPlus 167 | -------------------------------------------------------------------------------- /main3.py: -------------------------------------------------------------------------------- 1 | #-*- coding:utf-8 -*- 2 | import requests 3 | from bs4 import BeautifulSoup 4 | import os 5 | import sys, platform 6 | import warnings 7 | warnings.filterwarnings("ignore") 8 | system = platform.system() 9 | 10 | yellow, green, blue, purple, bluedark, gray, difficulties = [], [], [], [], [], [], [] 11 | colors = { '1r': 0, '2o': 0, '3y': 0, '4g': 0, '5b': 0, '6p': 0, '7d': 0, '8g': 0 } 12 | #topics = map(lambda x: x[1: -1], raw_input().split()) 13 | user = input() 14 | urlUser = 'https://www.luogu.org/space/show?uid=' + user 15 | urlBase = 'https://www.luogu.org/problemnew/show/' 16 | headers = { 17 | u'Referer': u'https://www.luogu.org/recordnew/lists?uid=10000', 18 | u'Accept': u'text/html, */*; q=0.01', 19 | u'Accept-Encoding': u'gzip, deflate, br', 20 | u'Accept-Language': u'zh-CN,zh;q=0.9', 21 | u'User-Agent': u'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36' 22 | } 23 | 24 | # 搞搞 UTF-8 25 | #reload(sys) 26 | #sys.setdefaultencoding('utf-8') 27 | #type = sys.getfilesystemencoding() 28 | 29 | f = open("d:\\LgData.txt", "w") # 仅用于统计这次爬虫运行耗费了多少流量 30 | 31 | # 获取个人主页信息 32 | v = requests.get(url = urlUser, headers = headers, verify = False) 33 | soup1 = BeautifulSoup(v.text, 'lxml') 34 | res = soup1.find('div', {'class': 'lg-article am-hide-sm'}) 35 | topics = res.text.split() 36 | for i in range(len(topics)): 37 | topics[i] = topics[i][1 : -1] 38 | 39 | # 第一个元素不是题目 40 | topics.pop(0) 41 | 42 | finished = 0 43 | 44 | for topic in topics: 45 | try: 46 | v = requests.get(url = urlBase + topic, headers = headers, verify = False, timeout = 10) 47 | except: 48 | #v = requests.get(url = urlBase + topic, headers = headers, verify = False, timeout = 10) 49 | #v = requests.get(url = urlBase + topic, headers = headers, verify = False, timeout = 10) 50 | print(topic, "Error!") 51 | continue 52 | 53 | #f.write(v.text) 54 | v = v.text#.decode('utf-8').encode(type) 55 | #print(v) 56 | 57 | soup = BeautifulSoup(v, 'lxml') 58 | try: 59 | difficulty = soup.findAll('span', {'class': 'lg-right'})[2].text # 获取难度 60 | difficulties.append(difficulty) 61 | # 不知道为什么,每次尝试用字典都爆异常,所以暴力 if 62 | if difficulty == '普及/提高-': 63 | print(topic, "Yellow") 64 | yellow.append(topic) 65 | elif difficulty == '普及+/提高': 66 | print(topic, "Green!") 67 | green.append(topic) 68 | elif difficulty == '提高+/省选-': 69 | print(topic, "Blue!!") 70 | blue.append(topic) 71 | elif difficulty == '省选/NOI-': 72 | print(topic, "Purple!!!") 73 | purple.append(topic) 74 | elif difficulty == 'NOI/NOI+/CTSC': 75 | print(topic, "BlueDark!!!!") 76 | bluedark.append(topic) 77 | elif difficulty == '尚无评定': 78 | print(topic, "Gray??") 79 | gray.append(topic) 80 | else: 81 | print(topic) 82 | except: 83 | print(topic, "Didn't allowed!") # 无权查看的题目 84 | 85 | finished += 1 86 | if system == "Windows": 87 | os.system('title U' + user + ': ' + str(finished) + '/' + str(len(topics))) 88 | 89 | print('------------------------------------') 90 | 91 | yellowPlus, greenPlus = 0, 0 92 | 93 | difficulties.sort() 94 | for i in difficulties: 95 | #print i#, d[i] 96 | if i == '入门难度': 97 | colors['1r'] += 1 98 | if i == '普及-': 99 | colors['2o'] += 1 100 | if i == '普及/提高-': 101 | colors['3y'] += 1 102 | yellowPlus += 1 103 | if i == '普及+/提高': 104 | colors['4g'] += 1 105 | yellowPlus += 1 106 | greenPlus += 1 107 | if i == '提高+/省选-': 108 | colors['5b'] += 1 109 | yellowPlus += 1 110 | greenPlus += 1 111 | if i == '省选/NOI-': 112 | colors['6p'] += 1 113 | yellowPlus += 1 114 | greenPlus += 1 115 | if i == 'NOI/NOI+/CTSC': 116 | colors['7d'] += 1 117 | yellowPlus += 1 118 | greenPlus += 1 119 | if i == '尚无评定': 120 | colors['8g'] += 1 121 | print('------------------------------------') 122 | 123 | print('Red:', colors['1r']) 124 | print('Orange:', colors['2o']) 125 | print('Yellow:', colors['3y']) 126 | print('Green:', colors['4g']) 127 | print('Blue:', colors['5b']) 128 | print('Purple:', colors['6p']) 129 | print('Bluedark:', colors['7d']) 130 | print('Gray:', colors['8g']) 131 | 132 | print('------------------------------------') 133 | 134 | print('\n' + str(colors['3y']), 'Yellow:') 135 | for i in range(len(yellow)): 136 | print(yellow[i], end = ' \n'[(i + 1) % 10 == 0]) 137 | print('\n' + str(colors['4g']), 'Green:') 138 | for i in range(len(green)): 139 | print(green[i], end = ' \n'[(i + 1) % 10 == 0]) 140 | print('\n' + str(colors['5b']), 'Blue:') 141 | for i in range(len(blue)): 142 | print(blue[i], end = ' \n'[(i + 1) % 10 == 0]) 143 | print('\n' + str(colors['6p']), 'Purple:') 144 | for i in range(len(purple)): 145 | print(purple[i], end = ' \n'[(i + 1) % 10 == 0]) 146 | print('\n' + str(colors['7d']), 'Bluedark:') 147 | for i in range(len(bluedark)): 148 | print(bluedark[i], end = ' \n'[(i + 1) % 10 == 0]) 149 | print('\n' + str(colors['8g']), 'Gray:') 150 | for i in range(len(gray)): 151 | print(gray[i], end = ' \n'[(i + 1) % 10 == 0]) 152 | print("\n") 153 | 154 | print('------------------------------------') 155 | 156 | print('Yellow+: ', yellowPlus) 157 | print('Green+: ', greenPlus) 158 | --------------------------------------------------------------------------------