朋友圈
pywxauto 支持朋友圈动态的浏览、点赞、评论和发布操作。
获取朋友圈动态
python
from pywxauto import Weixin
wx = Weixin()
# 获取最新 10 条动态
moments = wx.get_moments(count=10)
for item in moments:
print(f"[{item.type}] {item.sender}: {item.content}")
print(f" 时间: {item.timestamp}, 图片数: {item.image_count}")逐条遍历(生成器)
python
for item in wx.iter_moments(count=20):
print(item)点赞
python
moments = wx.get_moments(5)
# 对第一条动态点赞
wx.like_moment(moments[0])
# 取消点赞
wx.unlike_moment(moments[0])评论
python
moments = wx.get_moments(5)
wx.comment_moment(moments[0], "拍得真好!")条件批量点赞
python
# 对所有动态点赞
wx.like_moment_when(lambda count, item: True)
# 只点赞包含"旅行"的动态
wx.like_moment_when(lambda count, item: "旅行" in item.content)
# 只点赞"张三"发的
wx.like_moment_when(lambda count, item: item.sender == "张三")
# 点赞前 5 条后停止
wx.like_moment_when(lambda count, item: count < 5)条件批量评论
python
# 对所有动态评论"不错"
wx.comment_moment_when(lambda count, item: "不错")
# 根据内容生成评论
def gen_comment(count, item):
if "旅行" in item.content:
return "风景真美!"
if "美食" in item.content:
return "看起来好好吃"
return None # 跳过
wx.comment_moment_when(gen_comment)发布朋友圈
发布纯文字
python
wx.moment.publish(text="今天天气真好☀️")发布图文
python
wx.moment.publish(
text="周末出游 🌄",
images=["C:/photos/1.jpg", "C:/photos/2.jpg"],
)发布视频
python
wx.moment.publish(
text="记录美好时刻",
video="C:/videos/trip.mp4",
)设置隐私权限
python
wx.moment.publish(
text="仅好友可见",
permission="公开", # "公开" / "私密" / "谁可以看" / "不给谁看"
)
# 部分好友可见
wx.moment.publish(
text="分享给特定朋友",
permission="谁可以看",
permission_contacts=["张三", "李四"],
)
# 不给某些人看
wx.moment.publish(
text="不想让老板看到",
permission="不给谁看",
permission_contacts=["老板"],
)提醒谁看
python
wx.moment.publish(
text="@好友看看",
remind_contacts=["张三", "李四"],
)刷新朋友圈
python
wx.refresh_moment()截图动态
python
moments = wx.get_moments(1)
png_bytes = wx.capture_moment(moments[0])
with open("moment.png", "wb") as f:
f.write(png_bytes)滚动浏览
python
# 逐条向下滚动浏览
item = wx.moment.scroll_to_next()
while item:
print(item)
item = wx.moment.scroll_to_next()MomentItem 属性
| 属性 | 类型 | 说明 |
|---|---|---|
sender | str | 发布者昵称 |
content | str | 正文内容 |
type | str | 类型(文本/图片/视频/分享等) |
timestamp | str | 时间(如"8小时前"、"昨天") |
image_count | int | 图片数量 |
raw_text | str | 原始控件文本 |