import requests from bs4 import BeautifulSoup import time user_cookie = "" # 註意:抽屜網 cookies中的gpsd是最要的驗證數據 def login(phone_id, phone_pwd): global user_... ...
import requests from bs4 import BeautifulSoup import time user_cookie = "" # 註意:抽屜網 cookies中的gpsd是最要的驗證數據 def login(phone_id, phone_pwd): global user_cookie # 因為抽屜的cookie設置比較特別,其cookie為未登錄狀態下的cookie通過登錄成功來進行驗證,從而認證為登錄狀態下的cookie r1 = requests.get( url= 'https://dig.chouti.com/', headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', } ) user_cookie = r1.cookies.get_dict() r2 = requests.post( url= 'https://dig.chouti.com/login', headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', }, data = { "phone": "86%d" % int(phone_id), "password": phone_pwd, "oneMonth": 1 }, cookies = user_cookie ) # 返回認證後的cookie return user_cookie def thumbsup(): # 抽屜新熱榜只有120頁 i=1 while i <= 120: r3 = requests.get( url= 'https://dig.chouti.com/all/hot/recent/%d'% i, headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', }, cookies = user_cookie, ) print("正在給第%d頁點贊" % i,) soup = BeautifulSoup(r3.text, 'html.parser') res_list = soup.find_all(name='div', attrs={'class': 'item'}) for res in res_list: requests.post( url='https://dig.chouti.com/link/vote?linksId=%d' % int(res.find(name='img').attrs['lang']), headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', }, cookies=user_cookie ) time.sleep(0.5) i+=1 print("準備執行下一頁...") def main(): login("手機號", "密碼") thumbsup() if __name__ == "__main__": main()