python使用webdriver爬取微信公眾號
更新時間:2018年08月31日 15:30:37 作者:消滅八阿哥
這篇文章主要為大家詳細(xì)介紹了python使用webdriver爬取微信公眾號信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python使用webdriver爬取微信公眾號的具體代碼,供大家參考,具體內(nèi)容如下
# -*- coding: utf-8 -*- from selenium import webdriver import time import json import requests import re import random #微信公眾號賬號 user="" #公眾號密碼 password="" #設(shè)置要爬取的公眾號列表 gzlist=['香河微服務(wù)'] #登錄微信公眾號,獲取登錄之后的cookies信息,并保存到本地文本中 def weChat_login(): #定義一個空的字典,存放cookies內(nèi)容 post={} #用webdriver啟動谷歌瀏覽器 print("啟動瀏覽器,打開微信公眾號登錄界面") driver = webdriver.Chrome(executable_path='E:\\program\\chromedriver.exe') #打開微信公眾號登錄頁面 driver.get('https://mp.weixin.qq.com/') #等待5秒鐘 time.sleep(5) print("正在輸入微信公眾號登錄賬號和密碼......") #清空賬號框中的內(nèi)容 driver.find_element_by_xpath("http://*[@id=\"header\"]/div[2]/div/div/form/div[1]/div[1]/div/span/input").clear() #自動填入登錄用戶名 driver.find_element_by_xpath("http://*[@id=\"header\"]/div[2]/div/div/form/div[1]/div[1]/div/span/input").send_keys(user) #清空密碼框中的內(nèi)容 driver.find_element_by_xpath("http://*[@id=\"header\"]/div[2]/div/div/form/div[1]/div[2]/div/span/input").clear() #自動填入登錄密碼 driver.find_element_by_xpath("http://*[@id=\"header\"]/div[2]/div/div/form/div[1]/div[2]/div/span/input").send_keys(password) # 在自動輸完密碼之后需要手動點一下記住我 print("請在登錄界面點擊:記住賬號") time.sleep(10) #自動點擊登錄按鈕進(jìn)行登錄 driver.find_element_by_xpath("http://*[@id=\"header\"]/div[2]/div/div/form/div[4]/a").click() # 拿手機掃二維碼! print("請拿手機掃碼二維碼登錄公眾號") time.sleep(20) print("登錄成功") #重新載入公眾號登錄頁,登錄之后會顯示公眾號后臺首頁,從這個返回內(nèi)容中獲取cookies信息 driver.get('https://mp.weixin.qq.com/') #獲取cookies cookie_items = driver.get_cookies() #獲取到的cookies是列表形式,將cookies轉(zhuǎn)成json形式并存入本地名為cookie的文本中 for cookie_item in cookie_items: post[cookie_item['name']] = cookie_item['value'] cookie_str = json.dumps(post) with open('cookie.txt', 'w+') as f: f.write(cookie_str) print("cookies信息已保存到本地") #爬取微信公眾號文章,并存在本地文本中 def get_content(query): #query為要爬取的公眾號名稱 #公眾號主頁 url = 'https://mp.weixin.qq.com' #設(shè)置headers header = { "HOST": "mp.weixin.qq.com", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0" } #讀取上一步獲取到的cookies with open('cookie.txt', 'r') as f: cookie = f.read().decode("UTF-8") cookies = json.loads(cookie) #登錄之后的微信公眾號首頁url變化為:https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=1849751598,從這里獲取token信息 response = requests.get(url=url, cookies=cookies) token = re.findall(r'token=(\d+)', str(response.url))[0] #搜索微信公眾號的接口地址 search_url = 'https://mp.weixin.qq.com/cgi-bin/searchbiz?' #搜索微信公眾號接口需要傳入的參數(shù),有三個變量:微信公眾號token、隨機數(shù)random、搜索的微信公眾號名字 query_id = { 'action': 'search_biz', 'token' : token, 'lang': 'zh_CN', 'f': 'json', 'ajax': '1', 'random': random.random(), 'query': query, 'begin': '0', 'count': '5' } #打開搜索微信公眾號接口地址,需要傳入相關(guān)參數(shù)信息如:cookies、params、headers search_response = requests.get(search_url, cookies=cookies, headers=header, params=query_id) #取搜索結(jié)果中的第一個公眾號 lists = search_response.json().get('list')[0] #獲取這個公眾號的fakeid,后面爬取公眾號文章需要此字段 fakeid = lists.get('fakeid') #微信公眾號文章接口地址 appmsg_url = 'https://mp.weixin.qq.com/cgi-bin/appmsg?' #搜索文章需要傳入幾個參數(shù):登錄的公眾號token、要爬取文章的公眾號fakeid、隨機數(shù)random query_id_data = { 'token': token, 'lang': 'zh_CN', 'f': 'json', 'ajax': '1', 'random': random.random(), 'action': 'list_ex', 'begin': '0',#不同頁,此參數(shù)變化,變化規(guī)則為每頁加5 'count': '5', 'query': '', 'fakeid': fakeid, 'type': '9' } #打開搜索的微信公眾號文章列表頁 appmsg_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data) #獲取文章總數(shù) max_num = appmsg_response.json().get('app_msg_cnt') #每頁至少有5條,獲取文章總的頁數(shù),爬取時需要分頁爬 num = int(int(max_num) / 5) #起始頁begin參數(shù),往后每頁加5 begin = 0 while num + 1 > 0 : query_id_data = { 'token': token, 'lang': 'zh_CN', 'f': 'json', 'ajax': '1', 'random': random.random(), 'action': 'list_ex', 'begin': '{}'.format(str(begin)), 'count': '5', 'query': '', 'fakeid': fakeid, 'type': '9' } print('正在翻頁:--------------',begin) #獲取每一頁文章的標(biāo)題和鏈接地址,并寫入本地文本中 query_fakeid_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data) fakeid_list = query_fakeid_response.json().get('app_msg_list') for item in fakeid_list: content_link=item.get('link') content_title=item.get('title') fileName=query+'.txt' # with open('e://xhwfw.txt','a') as fh: # fh.write(content_title+":\n"+content_link+"\n") print content_title+":\n"+content_link+"\n" num -= 1 begin = int(begin) begin+=5 time.sleep(2) if __name__=='__main__': try: #登錄微信公眾號,獲取登錄之后的cookies信息,并保存到本地文本中 weChat_login() #登錄之后,通過微信公眾號后臺提供的微信公眾號文章接口爬取文章 for query in gzlist: #爬取微信公眾號文章,并存在本地文本中 print("開始爬取公眾號:"+query) get_content(query) print("爬取完成") except Exception as e: print(str(e))
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Python判斷整數(shù)是否是回文數(shù)的3種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于如何利用Python判斷整數(shù)是否是回文數(shù)的3種方總結(jié),回文數(shù)是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數(shù),需要的朋友可以參考下2021-07-07Python讀取Excel數(shù)據(jù)實現(xiàn)批量生成合同
Python在自動化辦公方面具有極大的優(yōu)勢,可以解決我們工作中遇到的很多重復(fù)性問題。本文將通過Python讀取Excel數(shù)據(jù)實現(xiàn)批量生成合同,需要的可以參考一下2022-05-05Pandas:DataFrame對象的基礎(chǔ)操作方法
今天小編就為大家分享一篇Pandas:DataFrame對象的基礎(chǔ)操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Pycharm 實現(xiàn)下一個文件引用另外一個文件的方法
今天小編就為大家分享一篇Pycharm 實現(xiàn)下一個文件引用另外一個文件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01

python 3.0 模擬用戶登錄功能并實現(xiàn)三次錯誤鎖定
Python的3.0版本,常被稱為Python 3000,或簡稱Py3k。這篇文章主要介紹了python 3.0 模擬用戶登錄功能并實現(xiàn)三次錯誤鎖定,需要的朋友可以參考下
2017-11-11