【原創(chuàng)】微博 關(guān)鍵詞 爬蟲
?本文所有教程及源碼、軟件僅為技術(shù)研究。不涉及計(jì)算機(jī)信息系統(tǒng)功能的刪除、修改、增加、干擾,更不會影響計(jì)算機(jī)信息系統(tǒng)的正常運(yùn)行。不得將代碼用于非法用途,如侵立刪!
記一次阿里云盾滑塊驗(yàn)證分析并通過
操作環(huán)境
win10 、 mac
Python3.9
數(shù)據(jù)接口
搜索
https://**********?containerid=100103type%3D{chanenl}%26q%3D{quote(self.words)}&page_type=searchall&page={page}
user info
https://**********?title=%E5%9F%BA%E6%9C%AC%E8%B5%84%E6%96%99&value={userid}'
proxy配置
==使用socks需安裝 :pip install -U 'requests[socks]'==
? ?def _proxy(self):
? ? ? ?"""
? ? ? ?調(diào)用
? ? ? ?:return:
? ? ? ?"""
? ? ? ?# 判斷IP是否過期
? ? ? ?new_time = datetime.datetime.now()
? ? ? ?if self.expire_time and new_time < datetime.datetime.strptime(self.expire_time, "%Y-%m-%d %H:%M:%S"):
? ? ? ? ? ?# # 當(dāng)前時間小于到期時間證明可用
? ? ? ? ? ?# if new_time < datetime.datetime.strptime(self.expire_time, "%Y-%m-%d %H:%M:%S"):
? ? ? ? ? ?print(f'當(dāng)前使用IP:{self.proxies} 過期時間:{self.expire_time}')
? ? ? ? ? ?return
? ? ? ?proxy = Proxy()
? ? ? ?ip_port, expire_time = proxy.main()
? ? ? ?self.headers = proxy.headers
? ? ? ?self.proxies = {
? ? ? ? ? ?'http': 'socks5://{}'.format(ip_port),
? ? ? ? ? ?'https': 'socks5://{}'.format(ip_port)
? ? ? ?}
? ? ? ?self.expire_time = expire_time
根據(jù)關(guān)鍵詞獲取userid
? ?def _get_userid(self, response):
? ? ? ?userid = []
? ? ? ?content = json.loads(response)
? ? ? ?cards = content['data']['cards'] ?# 數(shù)據(jù)列表
? ? ? ?for card in cards:
? ? ? ? ? ?if card['card_type'] != 11: ?# 狀態(tài)=11返回的是用戶數(shù)據(jù)列表
? ? ? ? ? ? ? ?continue
? ? ? ? ? ?for card_group in card['card_group']:
? ? ? ? ? ? ? ?userid.append(card_group['user']['id']) ?# 用戶id
? ? ? ?return userid
根據(jù)userid獲取信息
? ?def _parse_json(self, res):
? ? ? ?content = json.loads(res)
? ? ? ?data = {}
? ? ? ?data['用戶id'] = content['data']['userInfo']['id'] ?# userid
? ? ? ?data['用戶名'] = content['data']['userInfo']['screen_name'] ?# 用戶名
? ? ? ?# 性別
? ? ? ?sex = content['data']['userInfo']['gender']
? ? ? ?data['性別'] = '女' if sex == 'f' else '男'
? ? ? ?data['微博認(rèn)證名稱'] = content['data']['userInfo']['verified_reason'] ?# 微博認(rèn)證名稱
? ? ? ?data['簡介'] = content['data']['userInfo']['description'] ?# 簡介
? ? ? ?data['粉絲數(shù)量'] = content['data']['userInfo']['followers_count'] ?# 粉絲數(shù)量
? ? ? ?data['發(fā)布微博量'] = content['data']['userInfo']['statuses_count'] ?# 發(fā)布微博量
? ? ? ?data['關(guān)注量'] = content['data']['userInfo']['follow_count'] ?# 關(guān)注量
? ? ? ?data['用戶頭像'] = content['data']['userInfo']['profile_image_url'] ?# 用戶頭像
? ? ? ?data['移動端地址'] = content['data']['userInfo']['profile_url'] ?# 移動端地址
? ? ? ?data['關(guān)鍵詞'] = self.words
? ? ? ?return data
數(shù)據(jù)保存
? ?def _save_xls(self, data):
? ? ? ?"""
? ? ? ?保存數(shù)據(jù)
? ? ? ?data : 字典格式 必須和表頭長度一樣
? ? ? ?:return:
? ? ? ?"""
? ? ? ?# 判斷文件是否存在 如果存在則讀取然后插入新數(shù)據(jù),不存在則創(chuàng)建一個新DataFrame并添加表頭
? ? ? ?file = f'{PATH}/數(shù)據(jù)/關(guān)鍵詞-{self.words}.xlsx'
? ? ? ?Header = ['用戶id', '用戶名', '性別', '微博認(rèn)證名稱', '簡介', '粉絲數(shù)量', '發(fā)布微博量', '關(guān)注量', '用戶頭像', '移動端地址', '關(guān)鍵詞']
? ? ? ?if not os.path.exists(f'{PATH}/數(shù)據(jù)'):
? ? ? ? ? ?os.mkdir(f'{PATH}/數(shù)據(jù)')
? ? ? ?if not os.path.exists(file):
? ? ? ? ? ?# 創(chuàng)建一個新的文件 并寫入表頭
? ? ? ? ? ?df = pd.DataFrame(columns=Header)
? ? ? ?else:
? ? ? ? ? ?# 讀取現(xiàn)有文件
? ? ? ? ? ?df_read = pd.read_excel(file)
? ? ? ? ? ?df = pd.DataFrame(df_read)
? ? ? ?# 定義一行新數(shù)據(jù) data為一個字典
? ? ? ?new_data = pd.DataFrame(data, index=[1]) ?# 自定義索引為:1 ,這里也可以不設(shè)置index
? ? ? ?# 把定義的新數(shù)據(jù)添加到原數(shù)據(jù)最后一行 ignore_index=True,表示不按原來的索引,從0開始自動遞增
? ? ? ?df = df.append(new_data, ignore_index=True)
? ? ? ?# 保存數(shù)據(jù) sheet_name工作表名 index是否添加索引 header表頭
? ? ? ?df.to_excel(file, sheet_name=self.words, index=False, header=True)
數(shù)據(jù):
本文僅供學(xué)習(xí)交流使用,如侵立刪!