這篇文章主要介紹Python中selenium爬取微信公眾號文章的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
需求:
想閱讀微信公眾號歷史文章,但是每次找回看得地方不方便。
思路:
1、使用selenium打開微信公眾號歷史文章,并滾動刷新到最底部,獲取到所有歷史文章urls。
2、對urls進行遍歷訪問,并進行下載到本地。
實現
1、打開微信客戶端,點擊某個微信公眾號->進入公眾號->打開歷史文章鏈接(使用瀏覽器打開),并通過開發者工具獲取到cookies,保存為excel。

2、啟動webdriver,并添加相應cookies。
browser = webdriver.Chrome()
wait = WebDriverWait(browser,10)
# 隨便訪問一個地址,然后才能設置cookies
browser.get('https://httpbin.org/get')
# 添加cookies,df為保存的excel cookies
for i in range(len(df)):
cookie_dict = {
"domain": df.loc[i,'DomaiN'],
'name': df.loc[i,'Name'],
'value': str(df.loc[i,'Value']),
"expires": df.loc[i,"Expires/Max-Age"],
'path': '/',}
browser.add_cookie(cookie_dict)
browser.get(weixin_url)3、控制瀏覽器下移動
觀察page_source,可以發現,文章到最底部的判斷是。
<div class="loadmore with_line" id="js_nomore">
<div class="tips_wrp">
<span class="tips js_no_more_msg" >已無更多</span>
<span class="tips js_need_add_contact" >關注公眾帳號,接收更多消息</span>
</div>
</div>使用driver控制JS。
%%time
# 通過判斷已無更多的style,來判斷是否到最底部,最終執行到最底部
no_more_msg_style = 'display: none;'
while True:
wait.until(EC.presence_of_element_located((By.XPATH,'//span[@class="tips js_no_more_msg" and text()="已無更多"]')))
no_more= browser.find_element_by_xpath('//span[@class="tips js_no_more_msg" and text()="已無更多"]')
now_style = no_more.get_attribute('style')
if str(now_style).find(no_more_msg_style) == -1:
# 說明已經加載完了
break
else:
# 停頓一會,等待瀏覽器加載
time.sleep(5)
# 通過JS,執行到最底部
browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')4、關鍵信息獲取。
根據html,分析得出文章url處在<div msgid="1000000026">中。
<div class="weui_msg_card js_card" msgid="1000000026">
<div class="weui_msg_card_hd">2017年1月13日</div>
<div class="weui_msg_card_bd">
<!-- 圖文 -->
<!-- 普通圖文 -->
<div id="WXAPPMSG1000000026" class="weui_media_box appmsg js_appmsg" hrefs="http://mp.weixin.qq.com/s?__biz=MzI5MDQ4NzU5MA==&mid=2247483748&idx=1&sn=e804e638484794181a27c094f81be8e1&chksm=ec1e6d2ddb69e43bd3e1f554c2d0cedb37f099252f122cee1ac5052b589b56f428b2c304de8e&scene=38#wechat_redirect" data-t="0">
<span class="weui_media_hd js_media" data-s="640" hrefs="http://mp.weixin.qq.com/s?__biz=MzI5MDQ4NzU5MA==&mid=2247483748&idx=1&sn=e804e638484794181a27c094f81be8e1&chksm=ec1e6d2ddb69e43bd3e1f554c2d0cedb37f099252f122cee1ac5052b589b56f428b2c304de8e&scene=38#wechat_redirect" data-type="APPMSG">
</span>
<div class="weui_media_bd js_media" data-type="APPMSG">
<h5 class="weui_media_title" hrefs="http://mp.weixin.qq.com/s?__biz=MzI5MDQ4NzU5MA==&mid=2247483748&idx=1&sn=e804e638484794181a27c094f81be8e1&chksm=ec1e6d2ddb69e43bd3e1f554c2d0cedb37f099252f122cee1ac5052b589b56f428b2c304de8e&scene=38#wechat_redirect">
承認自己是難民有什么錯
</h5>
<p class="weui_media_desc">枷鎖已經足夠沉重,謝絕道德綁架</p>
<p class="weui_media_extra_info">2017年1月13日</p>
</div>
</div>
</div>
</div>文章類型主要分為,
<div class="weui_media_bd js_media" data-type="APPMSG">
<div class="weui_media_bd js_media" data-type="TEXT">
有無原創進行劃分。
最終實現:
%%time
result = []
errlist = []
# 先得到其中一個
el_divs = browser.find_elements_by_xpath('//div[@class="weui_msg_card_list"]/div[@class="weui_msg_card js_card"]')
i = 0
for div in el_divs:
date = title = url = yuanchuang = ''
try:
date = div.find_element_by_xpath('.//div[@class="weui_msg_card_hd"]').get_attribute('innerHTML')
el_content = div.find_element_by_xpath('.//div[@class="weui_media_bd js_media"]')
if el_content.get_attribute('data-type') == 'APPMSG':
el = el_content.find_element_by_xpath('./h5[@class="weui_media_title"]')
title = el.text
url = el.get_attribute('hrefs')
xb = el_content.find_element_by_xpath('./p[@class="weui_media_extra_info"]').text
yuanchuang = '原創' if xb.find('原創') != -1 else ''
elif el_content.get_attribute('data-type') == 'TEXT':
title = '隨文'
url = el_content.find_element_by_xpath('./div').text
yuanchuang = '原創'
else:
# 其他未能識別的類型
errlist.append([i,div.get_attribute('innerHTML')])
except NoSuchElementException:
errlist.append([i,div.get_attribute('innerHTML')])
print(str(i),':',date,title,url,yuanchuang)
result.append([date,title,yuanchuang,url])
i = i + 15、將得到url保存到excel
dfout = pd.DataFrame(result, columns=['日期', '標題', '原創', '地址'])
with pd.ExcelWriter(savename) as writer:
dfout.to_excel(writer,index=False,sheet_name = 'Sheet1')
最終保存形式

6、在遍歷最后的鏈接地址,逐個requets保存,即可得到。組建成菜單形式的文章,可參考
記一次 excel vba 參考手冊爬蟲實戰,不必要的一次爬蟲。:htthttps://www.jb51.net/article/193107.htm
遇到的坑:
1、find_element_by_xpath 需要配上 NoSuchElementException 使用,否則遇到未找到的節點就會出錯,最初find_elements_by_xpath 來防止找不到相關節點,結果發現,執行速度異常的慢,需要查找原因。
2、cookies使用的時候是人為獲取,如果太長時間不用,需要重新獲取??梢钥紤]結合pyautogui來控制weixin客戶端來進行獲取。?
3、構建的時候,最后分布試行,最初的文章類型沒有做好判斷,結果執行時間很久。做好異常捕獲,再逐步分析錯誤的節點問題。
以上是Python中selenium爬取微信公眾號文章的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。