最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python下載微信公眾號(hào)相關(guān)文章

 更新時(shí)間:2019年02月26日 09:34:16   作者:qd_tudou  
這篇文章主要為大家詳細(xì)介紹了python下載微信公眾號(hào)相關(guān)文章的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python下載微信公眾號(hào)相關(guān)文章的具體代碼,供大家參考,具體內(nèi)容如下

目的:從零開始學(xué)自動(dòng)化測(cè)試公眾號(hào)中下載“pytest"一系列文檔

1、搜索微信號(hào)文章關(guān)鍵字搜索

2、對(duì)搜索結(jié)果前N頁(yè)進(jìn)行解析,獲取文章標(biāo)題和對(duì)應(yīng)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
  # [...] 用來表示一組字符,單獨(dú)列出:[amk] 匹配 'a','m'或'k'
  # re+ 匹配1個(gè)或多個(gè)的表達(dá)式。
  self.pattern = r'[\\/:*?"<>|\r\n]+'
 
 def get_page_url(self):
  for i in range(1,self.pageno+1):
   # https://weixin.sogou.com/weixin?query=從零開始學(xué)自動(dòng)化測(cè)試&_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 > div[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):
  # 圖片的處理比較復(fù)雜,首先需要找到對(duì)應(yīng)的圖片的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_div == 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

運(yùn)行結(jié)果:

缺少部分文檔,如pytest文檔4,是因?yàn)樗压肺⑿盼恼滤阉鹘Y(jié)果中就沒有

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python 字典套字典或列表的示例

    python 字典套字典或列表的示例

    今天小編就為大家分享一篇python 字典套字典或列表的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python調(diào)用tcpdump抓包過濾的方法

    python調(diào)用tcpdump抓包過濾的方法

    這篇文章主要為大家詳細(xì)介紹了python調(diào)用tcpdump抓包過濾的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Python可視化學(xué)習(xí)之seaborn繪制矩陣圖詳解

    Python可視化學(xué)習(xí)之seaborn繪制矩陣圖詳解

    矩陣圖即用一張圖繪制多個(gè)變量之間的關(guān)系,數(shù)據(jù)挖掘中常用于初期數(shù)據(jù)探索。本文介紹python中seaborn.pairplot和seaborn.PairGrid繪制矩陣圖,需要的可以參考一下
    2022-02-02
  • 使用python批量讀取word文檔并整理關(guān)鍵信息到excel表格的實(shí)例

    使用python批量讀取word文檔并整理關(guān)鍵信息到excel表格的實(shí)例

    今天小編就為大家分享一篇使用python批量讀取word文檔并整理關(guān)鍵信息到excel表格的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python實(shí)現(xiàn)磁盤日志清理的示例

    python實(shí)現(xiàn)磁盤日志清理的示例

    這篇文章主要介紹了python實(shí)現(xiàn)磁盤日志清理的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python簡(jiǎn)單I/O操作示例

    Python簡(jiǎn)單I/O操作示例

    這篇文章主要介紹了Python簡(jiǎn)單I/O操作,結(jié)合實(shí)例形式分析了Python針對(duì)文件的I/O讀寫及cPickle模塊相關(guān)使用操作技巧,需要的朋友可以參考下
    2019-03-03
  • Python如何急速下載第三方庫(kù)詳解

    Python如何急速下載第三方庫(kù)詳解

    這篇文章主要給大家介紹了關(guān)于Python如何急速下載第三方庫(kù)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 基于python實(shí)現(xiàn)微信收紅包自動(dòng)化測(cè)試腳本(測(cè)試用例)

    基于python實(shí)現(xiàn)微信收紅包自動(dòng)化測(cè)試腳本(測(cè)試用例)

    這篇文章主要介紹了基于python實(shí)現(xiàn)微信收紅包自動(dòng)化測(cè)試腳本,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-07-07
  • Python中判斷對(duì)象是否為空的方法

    Python中判斷對(duì)象是否為空的方法

    在Python開發(fā)中,判斷對(duì)象是否為“空”是高頻操作,但看似簡(jiǎn)單的需求卻暗藏玄機(jī),從None到空容器,從零值到自定義對(duì)象的“假值”狀態(tài),不同場(chǎng)景下的“空”需要精準(zhǔn)區(qū)分,本文將系統(tǒng)梳理Python中“空”的判定邏輯,揭示常見誤區(qū),并提供實(shí)用解決方案,需要的朋友可以參考下
    2025-04-04
  • python生成隨機(jī)密碼或隨機(jī)字符串的方法

    python生成隨機(jī)密碼或隨機(jī)字符串的方法

    這篇文章主要介紹了python生成隨機(jī)密碼或隨機(jī)字符串的方法,涉及Python字符串及隨機(jī)數(shù)的相關(guān)使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-07-07

最新評(píng)論

油尖旺区| 福海县| 陆良县| 丰县| 桐梓县| 长岛县| 洛南县| 石家庄市| 梁河县| 庄浪县| 青冈县| 扎鲁特旗| 科尔| 沾化县| 周至县| 东宁县| 德阳市| 阳曲县| 平昌县| 土默特右旗| 故城县| 皋兰县| 齐河县| 郧西县| 正安县| 青岛市| 无极县| 沁源县| 中西区| 曲水县| 商南县| 额济纳旗| 鹤庆县| 亳州市| 庆城县| 天镇县| 屏东市| 托克逊县| 惠州市| 甘谷县| 巴马|