python微信公众号爬虫如何使用Python爬取微信公众号文章?

Python微信公众号爬虫是一种爬取微信公众号文章的工具,通过它可以获取某个公众号的所有文章,包括标题、时间、正文等内容。下面是一个简单的python微信公众号爬虫代码:

Python微信公众号爬虫是一种爬取微信公众号文章的工具,通过它可以获取某个公众号的所有文章,包括标题、时间、正文等内容。

下面是一个简单的python微信公众号爬虫代码:

import requests

from lxml import etree

# 请求头

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'

}

# 要爬取的微信公众号名称

wechat_name = ''

# 获取微信公众号文章列表

url = 'https://weixin.sogou.com/weixin?type=1&s_from=input&query={}&ie=utf8&_sug_=n&_sug_type_='.format(wechat_name)

response = requests.get(url, headers=headers)

html = etree.HTML(response.text)

article_urls = html.xpath('//div[@cl="txt-box"]/h3/a/@href')

# 爬取文章详情

for article_url in article_urls:

response = requests.get(article_url, headers=headers)

html = etree.HTML(response.text)

# 标题

title = html.xpath('//h2[@cl="rich_media_title"]/text()')

# 时间

time = html.xpath('//em[@id="post-date"]/text()')

# 正文

content = html.xpath('//div[@cl="rich_media_content "]//p/text()')

print(title, time, content)

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(854)
java跟python:如何利用Java和Python构建强大的编程能力
上一篇
python 读取配置一种快速、简单的方式
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(36条)