python-爬蟲:取qq號中各分組成員資訊存入資料庫,並將qq頭像下載儲存到檔案夾,圖片命名為qq號(執行個體3)

來源:互聯網
上載者:User

標籤:res   cgi   介面   srm   def   __init__   bin   requests   資料庫   

import requests
import pymongo
import requests
import os

class QqGroup:
#三個介面url 擷取 qq組號 擷取每群組成員資訊 擷取qq頭像
group_url = "https://qun.qq.com/cgi-bin/qun_mgr/get_group_list"
member_url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
pic_url = "http://q4.qlogo.cn/g?b=qq&nk={%s}&s=140"
#一般將cookie與連結資料庫代碼寫在建構函式
def __init__(self,cookie):
self.cookie = cookie
self.client = pymongo.MongoClient(host=‘118.24.3.40‘,port=27017)
self.db = self.client[‘qq_group‘] #資料庫名稱
self.table = self.db[‘qq_group‘] #表名稱
#擷取 qq組號
def get_all_group(self):
data = {‘bkn‘:2039193070}
res = requests.post(self.group_url,data,headers={‘cookie‘:self.cookie}).json()
#新方法 1、對於傳回值為多個list合并,如果返回為空白,增加預設值為空白 2、對於字典取值用get方法
all_groups = res.get(‘join‘, []) + res.get(‘create‘, []) + res.get(‘manage‘, [])

all_gc=[i.get(‘gc‘) for i in all_groups]

return all_gc
#擷取每群組成員資訊
def get_group_info(self,num):
data = {"gc":num,"st":0,"end":200,"sort":0,"bkn":2039193070}
res = requests.post(url=self.member_url, data=data, headers={‘Cookie‘:self.cookie})
mems = res.json().get("mems")
for i in mems:
mem = {
"qq": i.get("uin"),
"gender": i.get("g"),
"nick": i.get("nick"),
"card": i.get("card"),
"qage": i.get("qage")
}
if mem.get("gender") == 0:
mem[‘gender‘] = "男"
elif mem.get("gender") == 1:
mem[‘gender‘] = "女"
else:
mem[‘gender‘] = "未知"
if mem.get("card") == "":
mem[‘card‘] = "沒有群名片"
self.save_mongo(mem)
self.down_pic(i.get("uin"))
#存入mogodb資料庫
def save_mongo(self,data):
self.table.insert(data)

#下載qq頭像
def down_pic(self,qq_num):
real_url=self.pic_url %qq_num
res=requests.get(real_url).content
pathnew = os.path.dirname(os.path.abspath(__file__))
pathnew = os.path.join(pathnew, ‘imgs‘)
if not os.path.isdir(‘imgs‘):
os.makedirs(‘imgs‘)
with open(os.path.join(pathnew, str(qq_num) + ‘.jpg‘), ‘wb‘) as fw:
fw.write(res)
print(‘%s頭像下載完成‘ % qq_num)
#main函數用於將各過程統一到一個函數,便於執行個體化後調用
def mian(self):
all_group=self.get_all_group()
for i in all_group:
self.get_group_info(i)
#執行個體化類 參數傳遞為cookie
Q=QqGroup(‘pgv_pvid=1745803612; pgv_pvi=9485686784; RK=vRZxXdy1Y7; _qpsvr_localtk=0.8876927078641847; pgv_si=s3971407872; ptisp=cnc; ptcz=e42f943c39b23e3d7a94d9deac0de69388506bbb9b09000f197b8681624f70ac; uin=o0106148088; [email protected]; pt2gguin=o0106148088; p_uin=o0106148088; pt4_token=Vrwh5LwXxUAe0OaJi5DR-XEt8F13T5OheqfWVI3CJjQ_; p_skey=SPaNeBpdwjMfAGplMvT-msRMptEYPnPykjrJo80kXqY_‘)
Q.mian()

python-爬蟲:取qq號中各分組成員資訊存入資料庫,並將qq頭像下載儲存到檔案夾,圖片命名為qq號(執行個體3)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.