Mac上Python抓取微信朋友圈,数据轻松获取!

优采云 发布时间: 2023-06-01 02:40

  微信朋友圈是人们分享生活的重要平台mac python 抓取微信朋友圈,但是官方并没有提供API接口,如何抓取微信朋友圈数据?本文将介绍使用Python在Mac电脑上抓取微信朋友圈的方法。

  一、准备工作

  1.安装Python3和pip;

  2.安装Seleniummac python 抓取微信朋友圈,可通过pip安装;

  3.下载Chrome浏览器;

  4.下载Chrome浏览器对应版本的chromedriver驱动程序,并将其放置在系统路径下。

  二、登陆微信网页版

  使用Selenium在Chrome浏览器中打开微信网页版Mac上Python抓取微信朋友圈,数据轻松获取!,并进行扫码登录。代码如下:

  python

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('https://wx.qq.com/')

#等待扫码登录

  三、进入朋友圈页面

  登录成功后Mac上Python抓取微信朋友圈,数据轻松获取!,跳转到朋友圈页面。代码如下:

  python

#进入朋友圈页面

driver.get('https://wx.qq.com/?&lang=zh_CN#navBar')

#点击发现按钮进入朋友圈页面

find_button = driver.find_element_by_xpath("//div[@class='menuitem___2l5eZ menuitem__show_dot___2gvoz' and @title='发现']")

find_button.click()

moment_button = driver.find_element_by_xpath("//li[@class='menu_item js_menu_item']//span[text()='朋友圈']")

moment_button.click()

  四、滚动页面

  由于朋友圈内容是动态加载的,需要通过模拟鼠标滚动来加载更多内容。代码如下:

  python

import time

#模拟滚动鼠标

for i in range(10):

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

time.sleep(2)

  五、获取朋友圈内容

  

  通过Selenium获取页面元素,并解析出朋友圈内容。代码如下:

  python

from bs4 import BeautifulSoup

#获取页面源码并解析

soup = BeautifulSoup(driver.page_source,'html.parser')

moments = soup.find_all('div',{'class':'weui_media_box appmsg'})

#解析朋友圈内容

for moment in moments:

#获取朋友圈文本内容和图片链接

content = moment.find('p',{'class':'weui_media_desc'}).text.strip()

images =[img['data-src'] for img in moment.find_all('img')]

print(content, images)

  六、处理登录过期问题

  微信网页版登录有时效性,需要定时重新扫码登录。代码如下:

  python

import datetime

#判断是否登录过期,过期后重新扫码登录

def is_login_expired():

try:

driver.find_element_by_xpath("//div[@class='menuitem___2l5eZ menuitem__show_dot___2gvoz' and @title='发现']")

return False

except:

return True

while True:

#登录过期则重新扫码登录

if is_login_expired():

driver.get('https://wx.qq.com/')

#等待扫码登录

else:

#获取朋友圈内容

get_moments()

time.sleep(60)

  七、优化抓取效率

  为了提高抓取效率,可以使用多线程或异步IO的方式。代码如下:

  python

import asyncio

import aiohttp

#异步获取朋友圈内容

async def get_moments_async(session):

async with session.get('https://wx.qq.com/?&lang=zh_CN#navBar') as response:

soup = BeautifulSoup(await response.text(),'html.parser')

moments = soup.find_all('div',{'class':'weui_media_box appmsg'})

for moment in moments:

content = moment.find('p',{'class':'weui_media_desc'}).text.strip()

images =[img['data-src'] for img in moment.find_all('img')]

print(content, images)

#使用异步IO方式获取朋友圈内容

async def main_async():

async with aiohttp.ClientSession() as session:

async with session.get('https://wx.qq.com/') as response:

#等待扫码登录

while True:

if is_login_expired():

#重新扫码登录

pass

else:

tasks =[get_moments_async(session) for i in range(10)]

await asyncio.gather(*tasks)

await asyncio.sleep(60)

#启动异步IO方式抓取朋友圈内容

loop = asyncio.get_event_loop()

loop.run_until_complete(main_async())

  八、注意事项

  1.抓取微信朋友圈数据需要遵循法律法规,不得盗取他人隐私信息;

  2.抓取频率不宜过高,以免被微信封号。

  九、总结

  本文介绍了在Mac电脑上使用Python抓取微信朋友圈的方法。通过Selenium模拟浏览器操作,获取朋友圈内容,并通过多线程或异步IO方式提高抓取效率。希望本文对大家有所帮助。

  十、优采云,SEO优化,www.ucaiyun.com

  想要让更多人看到你的文章吗?优采云为您提供专业的SEO优化服务,让您的文章更容易被搜索引擎收录和排名。详情请访问www.ucaiyun.com。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线