Created
February 23, 2020 09:18
-
-
Save cwt8805/ea91a1531aec3c6fbb0534e32532c919 to your computer and use it in GitHub Desktop.
一键关注所有绿帽子大学用户
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| loginData = {'session[email]': '你的登录邮箱', | |
| 'session[password]': '你的账号密码'} | |
| baseURL = "https://lmzdx.net" | |
| session = requests.Session() | |
| followingUsers = [] | |
| allUsers = [] | |
| def getUsersFromPage(page, tag): | |
| result = [] | |
| startIndex = page.find(tag) | |
| if startIndex == -1: | |
| return result | |
| startIndex += len(tag) | |
| endIndex = page.find('</ul>', startIndex) | |
| userList = page[startIndex:endIndex] | |
| startIndex = 0 | |
| while True: | |
| startIndex = userList.find('/users/', startIndex) | |
| if startIndex == -1: | |
| break | |
| startIndex += 7 | |
| endIndex = userList.find('"', startIndex) | |
| result.append(userList[startIndex:endIndex]) | |
| startIndex = endIndex + 1 | |
| return result | |
| print('正在登陆...') | |
| loginPage = session.get(baseURL + "/login").text | |
| tokenStartIndex = loginPage.find('name="authenticity_token"') | |
| tokenStartIndex = loginPage.find("value=", tokenStartIndex) + 7 | |
| tokenEndIndex = loginPage.find('"', tokenStartIndex) | |
| loginData["authenticity_token"] = loginPage[tokenStartIndex:tokenEndIndex] | |
| indexPage = session.post(baseURL + "/login", data=loginData).text | |
| print('正在获取用户ID...', end='') | |
| myUserIdStartIndex = indexPage.find('/users/') + 7 | |
| myUserIdEndIndex = indexPage.find('"', myUserIdStartIndex) | |
| myUserId = indexPage[myUserIdStartIndex:myUserIdEndIndex] | |
| followingUsers.append(myUserId) | |
| print(myUserId) | |
| for i in range(1, 100): | |
| page = session.get(baseURL + '/users/%s/following?page=%d' % (myUserId, i)).text | |
| users = getUsersFromPage(page, '<ul class="users follow">') | |
| if len(users) == 0: | |
| break | |
| print('已获取关注列表...page=%d' % i) | |
| followingUsers.extend(users) | |
| print('你当前共关注了%d名用户,正在检查新用户...' % (len(followingUsers)-1)) | |
| for i in range(1, 100): | |
| page = session.get(baseURL + '/users?page=%d' % i).text | |
| users = getUsersFromPage(page, '<ul class="users">') | |
| if len(users) == 0: | |
| break | |
| print('已获取用户列表...page=%d' % i) | |
| allUsers.extend(users) | |
| for userId in allUsers: | |
| if userId not in followingUsers: | |
| print('正在关注用户', end='...') | |
| page = session.get(baseURL + '/users/%s'%userId).text | |
| startIndex = page.find('img alt') + 9 | |
| endIndex = page.find('"', startIndex) | |
| print(page[startIndex:endIndex]) | |
| followData = {'followed_id': userId} | |
| startIndex = page.find('name="authenticity_token"', endIndex) | |
| startIndex = page.find("value=", startIndex) + 7 | |
| endIndex = page.find('"', startIndex) | |
| followData["authenticity_token"] = page[startIndex:endIndex] | |
| session.post(baseURL+'/relationships', data=followData) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
亦可赛艇