querylist采集微信公眾號文章(貼個(gè)代碼4.新建一個(gè)py文件獲取cookies文件 )
優(yōu)采云 發(fā)布時(shí)間: 2022-01-17 13:21querylist采集微信公眾號文章(貼個(gè)代碼4.新建一個(gè)py文件獲取cookies文件
)
通過(guò)微信公眾平臺的搜索文章界面,先抓取我們需要的相關(guān)文章1.,我們先來(lái)看看,正常登錄我們的微信公眾號,然后使用文章搜索功能,搜索我們需要查找的相關(guān)文章。 2.實(shí)現思路3.獲取cookie,不多說(shuō),貼個(gè)代碼
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
from selenium import webdriver
import time
import json
driver = webdriver.Chrome() #需要一個(gè)谷歌驅動(dòng)chromedriver.exe,要支持你當前谷歌瀏覽器的版本
driver.get('https://mp.weixin.qq.com/') #發(fā)起get請求打開(kāi)微信公眾號平臺登錄頁(yè)面,然后輸入賬號密碼登錄微信公眾號
driver.find_element_by_xpath('//*[@id="header"]/div[2]/div/div/form/div[1]/div[1]/div/span/input').clear() #定位到賬號輸入框,清除里面的內容
driver.find_element_by_xpath('//*[@id="header"]/div[2]/div/div/form/div[1]/div[1]/div/span/input').send_keys('這里輸入你的賬號') #定位到賬號輸入框,輸入賬號
time.sleep(3) #等待3秒后執行下一步操作,避免因為網(wǎng)絡(luò )延遲,瀏覽器來(lái)不及加載出輸入框,從而導致以下的操作失敗
driver.find_element_by_xpath('//*[@id="header"]/div[2]/div/div/form/div[1]/div[2]/div/span/input').clear() #定位到密碼輸入框,清除里面的內容
driver.find_element_by_xpath('//*[@id="header"]/div[2]/div/div/form/div[1]/div[2]/div/span/input').send_keys('這里輸入你的密碼') #定位到密碼輸入框,輸入密碼
time.sleep(3) #原因和以上相同
driver.find_element_by_xpath('//*[@id="header"]/div[2]/div/div/form/div[3]/label').click() #點(diǎn)擊記住密碼
time.sleep(3) #原因和以上相同
driver.find_element_by_xpath('//*[@id="header"]/div[2]/div/div/form/div[4]/a').click() #點(diǎn)擊登錄
time.sleep(15) #15秒內掃碼登錄
cookies = driver.get_cookies() #獲取掃碼登錄成功之后的cookies
print(cookies) #打印出來(lái)看看,如果超時(shí)了還不掃碼,獲取到的cookies是不完整的,不能用來(lái)登錄公眾號,所以第一次必須掃碼登錄以獲取完整的cookies
cookie = {} #定義一個(gè)空字典,以便把獲取的cookies以字典的形式寫(xiě)入
for items in cookies: #把登錄成功后獲取的cookies提取name和value參數寫(xiě)入空字典cookie
cookie[items.get('name')] = items.get('value')
with open('cookies.txt','w') as file: #新建并打開(kāi)一個(gè)cookies.txt文件
file.write(json.dumps(cookie)) #寫(xiě)入轉成字符串的字典
driver.close() #關(guān)閉瀏覽器
4.新建一個(gè)py文件,代碼如下
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
import requests
import json
import re #正則模塊
import random #隨機數模塊
import time
#query = 'python'
#讀取之前登錄后保存的cookies
with open('cookies.txt','r') as file:
cookie = file.read()
url = 'https://mp.weixin.qq.com/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'Referer': 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=10&share=1&token=773059916&lang=zh_CN',
'Host': 'mp.weixin.qq.com',
}
cookies = json.loads(cookie) #加載之前獲取的cookies
print(cookies) #可以打印看看,和之前保存的cookies是一樣的
response = requests.get(url, cookies = cookies) #請求https://mp.weixin.qq.com/,傳cookies參數,登錄成功
token = re.findall(r'token=(\d+)',str(response.url))[0] #登錄成功后,這是的url里是包含token的,要把token參數拿出來(lái),方便后面構造data數據包發(fā)起post請求
#print(token)
#random.random()返回0到1之間隨機數
#構造data數據包發(fā)起post請求
data = {
'token': token,
'lang': 'zh_CN',
'f': 'json',
'ajax': '1',
'random': random.random(),
'url': 'python',
'begin': '0',
'count': '3',
}
search_url = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=check_appmsg_copyright_stat' #按F12在瀏覽器里找post請求的url(搜索文章請求的url)
search_response = requests.post(search_url, cookies=cookies, data=data, headers=headers) #發(fā)起post請求,傳cookies、data、headers參數
max_num = search_response.json().get('total') #獲取所有文章的條數
num = int(int(max_num/3)) #每頁(yè)顯示3篇文章,要翻total/3頁(yè),不過(guò)實(shí)際上我搜索了幾個(gè)關(guān)鍵詞,發(fā)現微信公眾號文章搜索的接口最多顯示667頁(yè),其實(shí)后面還有頁(yè)數,max_num/3的結果大于667沒(méi)關(guān)系
if __name__ == '__main__':
query = input('請輸入你要搜索的內容:')
begin = 0
while num +1 > 0:
print(begin)
data = {
'token': token,
'lang': 'zh_CN',
'f': 'json',
'ajax': '1',
'random': random.random(),
'url': query,
'begin': '{}'.format(str(begin)),
'count': '3',
}
search_response = requests.post(search_url, cookies=cookies, data=data, headers=headers)
contentt = search_response.json().get('list') #list里面是我們需要的內容,所以要獲取list
for items in contentt: #具體需要list里面的哪些參數可以自己選擇,這里只獲取title、url、nickname、author
f = open('search.txt',mode='a',) #打開(kāi)一個(gè)txt文檔,把獲取的內容寫(xiě)進(jìn)去,mode='a'是追加的方式寫(xiě)入,不覆蓋
print('文章標題:',items.get('title')) #獲取文章標題
f.write('文章標題:')
f.write(items.get('title'))
f.write("\n")
f.write('文章url:')
f.write(items.get('url'))
f.write("\n")
f.write('公眾號:')
f.write(items.get('nickname'))
f.write("\n")
f.write('作者:')
f.write(items.get('author'))
f.write("\n")
f.write("\n")
print('文章url:',items.get('url')) #獲取文章的url
print('公眾號:',items.get('nickname')) #獲取出自哪個(gè)微信公眾號
print('文章作者:',items.get('author')) #獲取文章作者
num -= 1
begin = int(begin)
begin += 3
time.sleep(3)