亚洲国产精品无码久久大片,亚洲AV无码乱码麻豆精品国产,亚洲品质自拍网站,少妇伦子伦精品无码STYLES,国产精久久久久久久

python crawler_WeChat公共帳戶(hù)推送信息爬取示例

優(yōu)采云 發(fā)布時(shí)間: 2020-08-07 04:12

  python crawler_WeChat公共帳戶(hù)推送信息爬取示例

  更新時(shí)間: 2017年10月23日10:03:08轉載作者: ChaseChoi

  下面的編輯器將為您帶來(lái)python crawler_WeChat公共帳戶(hù)推送信息爬取的示例. 編輯認為這還不錯,因此我將與您分享并提供參考. 讓我們跟隨編輯器看看

  問(wèn)題描述

  使用搜狗的微信搜索來(lái)獲取指定官方帳戶(hù)的最新推送,并將相應的網(wǎng)頁(yè)保存到本地.

  注釋

  搜狗微信獲取的地址是臨時(shí)鏈接,對時(shí)間敏感.

  官方帳戶(hù)是一個(gè)動(dòng)態(tài)網(wǎng)頁(yè)(由JavaScript渲染),并且使用request.get()獲得的內容不收錄推送消息. 在這里,使用selenium + PhantomJS進(jìn)行處理

  代碼

  

#! /usr/bin/env python3

from selenium import webdriver

from datetime import datetime

import bs4, requests

import os, time, sys

# 獲取公眾號鏈接

def getAccountURL(searchURL):

res = requests.get(searchURL)

res.raise_for_status()

soup = bs4.BeautifulSoup(res.text, "lxml")

# 選擇第一個(gè)鏈接

account = soup.select('a[uigs="account_name_0"]')

return account[0]['href']

# 獲取首篇文章的鏈接,如果有驗證碼返回None

def getArticleURL(accountURL):

browser = webdriver.PhantomJS("/Users/chasechoi/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs")

# 進(jìn)入公眾號

browser.get(accountURL)

# 獲取網(wǎng)頁(yè)信息

html = browser.page_source

accountSoup = bs4.BeautifulSoup(html, "lxml")

time.sleep(1)

contents = accountSoup.find_all(hrefs=True)

try:

partitialLink = contents[0]['hrefs']

firstLink = base + partitialLink

except IndexError:

firstLink = None

print('CAPTCHA!')

return firstLink

# 創(chuàng )建文件夾存儲html網(wǎng)頁(yè),以時(shí)間命名

def folderCreation():

path = os.path.join(os.getcwd(), datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))

try:

os.makedirs(path)

except OSError as e:

if e.errno != errno.EEXIST:

raise

print("folder not exist!")

return path

# 將html頁(yè)面寫(xiě)入本地

def writeToFile(path, account, title):

myfile = open("{}/{}_{}.html".format(path, account, title), 'wb')

myfile.write(res.content)

myfile.close()

base ='https://mp.weixin.qq.com'

accountList = ['央視新聞', '新浪新聞','鳳凰新聞','羊城晚報']

query = 'http://weixin.sogou.com/weixin?type=1&s_from=input&query='

path = folderCreation()

for index, account in enumerate(accountList):

searchURL = query + account

accountURL = getAccountURL(searchURL)

time.sleep(10)

articleURL = getArticleURL(accountURL)

if articleURL != None:

print("#{}({}/{}): {}".format(account, index+1, len(accountList), accountURL))

# 讀取第一篇文章內容

res = requests.get(articleURL)

res.raise_for_status()

detailPage = bs4.BeautifulSoup(res.text, "lxml")

title = detailPage.title.text

print("標題: {}\n鏈接: {}\n".format(title, articleURL))

writeToFile(path, account, title)

else:

print('{} files successfully written to {}'.format(index, path))

sys.exit()

print('{} files successfully written to {}'.format(len(accountList), path))

  參考輸出

  端子輸出

  

  查找器

  

  分析

  獲取鏈接

  首先進(jìn)入搜狗的微信搜索頁(yè)面,在地址欄中提取鏈接的必需部分,將字符串連接到官方帳戶(hù)名,然后生成請求鏈接

  對于靜態(tài)網(wǎng)頁(yè),請使用請求獲取html文件,然后使用BeautifulSoup選擇所需的內容

  對于動(dòng)態(tài)網(wǎng)頁(yè),請使用selenium + PhantomJS獲取html文件,然后使用BeautifulSoup選擇所需的內容

  遇到驗證碼(CAPTCHA)時(shí),輸出提示. 此版本的代碼實(shí)際上并未處理驗證碼,因此在運行程序之前需要手動(dòng)對其進(jìn)行訪(fǎng)問(wèn),以避免驗證碼.

  文件寫(xiě)入

  使用os.path.join()構造存儲路徑可以提高通用性. 例如,Windows路徑分隔符使用反斜杠(\),而OS X和Linux使用反斜杠(/). 該功能可以根據平臺自動(dòng)轉換.

  open()使用b(二進(jìn)制模式)參數來(lái)提高通用性(適用于Windows)

  使用datetime.now()獲取當前的命名時(shí)間,并通過(guò)strftime()格式化時(shí)間(函數名稱(chēng)中的f表示格式),

  有關(guān)特定用途,請參閱下表(摘自“使用Python自動(dòng)完成鉆孔”)

  

  上面的python爬網(wǎng)程序_WeChat公共帳戶(hù)推送信息爬網(wǎng)示例是編輯器共享的所有內容,希望為您提供參考,也希望可以支持腳本庫.

0 個(gè)評論

要回復文章請先登錄注冊


官方客服QQ群

微信人工客服

QQ人工客服


線(xiàn)

亚洲国产精品无码久久大片,亚洲AV无码乱码麻豆精品国产,亚洲品质自拍网站,少妇伦子伦精品无码STYLES,国产精久久久久久久