python有什么好的微信公众号(python下载微信公众号相关文章)
类别:脚本大全 浏览量:1631
时间:2022-01-20 00:41:47 python有什么好的微信公众号
python下载微信公众号相关文章本文实例为大家分享了python下载微信公众号相关文章的具体代码,供大家参考,具体内容如下
目的:从零开始学自动化测试公众号中下载“pytest"一系列文档
1、搜索微信号文章关键字搜索
2、对搜索结果前n页进行解析,获取文章标题和对应url
主要使用的是requests和bs4中的beautifulsoup
weixin.py
|
import requests from urllib.parse import quote from bs4 import beautifulsoup import re from weixinspider.html2doc import myhtmlparser class weixinspider( object ): def __init__( self , gzh_name, pageno,keyword): self .gzh_name = gzh_name self .pageno = pageno self .keyword = keyword.lower() self .page_url = [] self .article_list = [] self .headers = { 'user-agent' : 'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.110 safari/537.36' } self .timeout = 5 # [...] 用来表示一组字符,单独列出:[amk] 匹配 'a','m'或'k' # re+ 匹配1个或多个的表达式。 self .pattern = r '[\\/:*?"<>|\r\n]+' def get_page_url( self ): for i in range ( 1 , self .pageno + 1 ): # https://weixin.sogou.com/weixin?query=从零开始学自动化测试&_sug_type_=&s_from=input&_sug_=n&type=2&page=2&ie=utf8 url = "https://weixin.sogou.com/weixin?query=%s&_sug_type_=&s_from=input&_sug_=n&type=2&page=%s&ie=utf8" \ % (quote( self .gzh_name),i) self .page_url.append(url) def get_article_url( self ): article = {} for url in self .page_url: response = requests.get(url,headers = self .headers,timeout = self .timeout) result = beautifulsoup(response.text, 'html.parser' ) articles = result.select( 'ul[class="news-list"] > li > li[class="txt-box"] > h3 > a ' ) for a in articles: # print(a.text) # print(a["href"]) if self .keyword in a.text.lower(): new_text = re.sub( self .pattern,"",a.text) article[new_text] = a[ "href" ] self .article_list.append(article) headers = { 'user-agent' : 'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.110 safari/537.36' } timeout = 5 gzh_name = 'pytest文档' my_gzh = weixinspider(gzh_name, 5 , 'pytest' ) my_gzh.get_page_url() # print(my_gzh.page_url) my_gzh.get_article_url() # print(my_gzh.article_list) for article in my_gzh.article_list: for (key,value) in article.items(): url = value html_response = requests.get(url,headers = headers,timeout = timeout) myhtmlparser = myhtmlparser(key) myhtmlparser.feed(html_response.text) myhtmlparser.doc.save(myhtmlparser.docfile) |
html2doc.py
|
from html.parser import htmlparser import requests from docx import document import re from docx.shared import rgbcolor import docx class myhtmlparser(htmlparser): def __init__( self ,docname): htmlparser.__init__( self ) self .docname = docname self .docfile = r "d:\pytest\%s.doc" % self .docname self .doc = document() self .title = false self .code = false self .text = '' self .processing = none self .codeprocessing = none self .picindex = 1 self .headers = { 'user-agent' : 'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.110 safari/537.36' } self .timeout = 5 def handle_startendtag( self , tag, attrs): # 图片的处理比较复杂,首先需要找到对应的图片的url,然后下载并写入doc中 if tag = = "img" : if len (attrs) = = 0 : pass else : for (variable, value) in attrs: if variable = = "data-type" : picname = r "d:\pytest\%s%s.%s" % ( self .docname, self .picindex, value) # print(picname) if variable = = "data-src" : picdata = requests.get(value, headers = self .headers, timeout = self .timeout) # print(value) self .picindex = self .picindex + 1 # print(self.picindex) with open (picname, "wb" ) as pic: pic.write(picdata.content) try : self .doc.add_picture(picname) except docx.image.exceptions.unexpectedendoffileerror as e: print (e) def handle_starttag( self , tag, attrs): if re.match(r "h(\d)" , tag): self .title = true if tag = = "p" : self .processing = tag if tag = = "code" : self .code = true self .codeprocessing = tag def handle_data( self , data): if self .title = = true: self .doc.add_heading(data, level = 2 ) # if self.in_li == true and self.tag == "p": if self .processing: self .text = self .text + data if self .code = = true: p = self .doc.add_paragraph() run = p.add_run(data) run.font.color.rgb = rgbcolor( 111 , 111 , 111 ) def handle_endtag( self , tag): self .title = false # self.code = false if tag = = self .processing: self .doc.add_paragraph( self .text) self .processing = none self .text = '' if tag = = self .codeprocessing: self .code = false |
运行结果:
缺少部分文档,如pytest文档4,是因为搜狗微信文章搜索结果中就没有
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/yaoliuwei1426/article/details/84707163
您可能感兴趣
- python有什么好的微信公众号(python下载微信公众号相关文章)
- 如何用微信公众号发送信息(微信公众号之主动给用户发送消息功能)
- python如何抓取公众号文章(python爬取微信公众号文章的方法)
- 微信公众号扫码跳转网页(微信公众号实现扫码获取微信用户信息网页授权)
- 微信公众号怎么获取用户openid(微信公众平台开发教程①获取用户Openid及个人信息图文详解)
- 开发者微信公众号开发流程(微信公众平台开发教程⑥ 微信开发集成类的使用图文详解)
- h5页面强制关注微信公众号(Html5页面获取微信公众号的openid的方法)
- php微信公众号管理后台(php实现微信公众号创建自定义菜单功能的实例代码)
- 微信公众号用户怎么增加(微信公众号用户与网站用户的绑定解决方案分析)
- h5怎么在微信公众号发布(H5微信公众号授权的简单实现步骤)
- 微信公众号文章 爬虫(python抓取搜狗微信公众号文章)
- 微信公众号怎么申请支付接口(微信公众平台开发教程③ PHP实现微信公众号支付功能图文详解)
- 微信公众号怎么开发页面(微信公众平台开发教程②微信端分享功能图文详解)
- 怎么做好SEO(怎么做好seo内容优化)
- 冬季钓鱼子线用 长 还是 短(冬季钓鱼子线用)
- 鱼竿 夏钓短,冬钓长 ,一定是这样 认清优缺点在选竿(鱼竿夏钓短冬钓长)
- 鲢鳙钓底还是钓浮 流水的水域应怎样做钓(鲢鳙钓底还是钓浮)
- 入秋后的第二场苹果发布会来了 全新M1系列芯片登场(入秋后的第二场苹果发布会来了)
- 苹果正式发布自研芯片M1 5nm 32核心 彻底放弃Intel(苹果正式发布自研芯片M1)
热门推荐
- jenkins 设置gitlab(jenkins+gitlab+nginx部署前端应用实现)
- php集成支付(ThinkPHP框架整合微信支付之刷卡模式图文详解)
- sqlserver2000安装之后在哪打开(SQL2000安装后,SQL Server组无项目解决方法)
- 云服务器选择七个原则(如何选择一个好的云服务器?)
- apache设置站点(Apache 多端口多站点配置方法)
- 如何减少用户操作网站时的出错率
- eval在python里面什么意思(python 为什么说eval要慎用)
- python如何获取微信好友(利用Python查看微信共同好友功能的实现代码)
- Html5新增的标签
- 云服务器磁盘管理教程(云主机如何用虚拟磁盘扩容?)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9