python 跨知乎app發私信以及Python專欄30萬用戶資訊爬取
阿新 • • 發佈:2018-11-14
import requests class SendMsg: def __init__(self): self.url='https://www.zhihu.com/api/v4/messages' #要傳送的資訊 self.data={'content':'小管家你好', 'receiver_hash':'****',#這裡是每個使用者的雜湊值ID 'type':'common' } self.cookie={****} def postmsg(self): '''主要用於傳送私信的方法requests.request(method, url, **kwargs) 支援各種方法 400請求錯誤 401驗證不通過 用data還是json提交,看請求頭content_type是不是json cookies中需要token,不能從原始頭複製''' html=requests.request('POST',self.url,json=self.data,headers=self.header,cookies=self.cookie) print(html.status_code) print(html.json()) for i in range(2): sending=SendMsg() sending.postmsg() #附上一個知乎使用者資料爬取 import requests class GetMsg: '''獲取粉絲資訊''' def __init__(self): self.cookie = {'*****'} self.header={'User-Agent':'***', } def getMag(self): for i in range(120,140,20): url='https://www.zhihu.com/api/v4/topics/19552832/followers?include=data[*]' \ '.gender,answer_count,articles_count,follower_count,' \ 'is_following,is_followed&limit=20&offset={}'.format(i) html=requests.request('GET',url=url,cookies=self.cookie,headers=self.header) print(html.status_code) content=html.json() for c in content['data']: if c['gender']==1: print('男') else: print('女') print(c['name'], c['headline']) a=GetMsg() a.getMag()