欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)快速保存微信公眾號文章中的圖片

 更新時間:2022年06月22日 15:12:16   作者:用余生去守護(hù)  
這篇文章主要為大家詳細(xì)介紹了如何利用Python語言實(shí)現(xiàn)快速保存微信公眾號文章中的圖片,文中的示例代碼講解詳細(xì),感興趣的可以嘗試一下

一、實(shí)現(xiàn)效果(以槿泉壁紙為例)

二、實(shí)現(xiàn)過程

1.新建一個link文本,將需要下載的文章鏈接依次保存;

2.新建一個.py文件,將下面的源碼復(fù)制進(jìn)去;

3.新建一個pic文件夾,用來保存圖片;

4.運(yùn)行即可;

三、源碼

sound code

代碼如下(示例):

import requests
from re import findall
from bs4 import BeautifulSoup
import time
import os
import sys


weixin_title=""
weixin_time=""

#獲取微信公眾號內(nèi)容,保存標(biāo)題和時間
def get_weixin_html(url):
    global weixin_time,weixin_title
    res=requests.get(url)
    soup=BeautifulSoup(res.text,"html.parser")
    
    #獲取標(biāo)題
    temp=soup.find('h1')
    weixin_title=temp.string.strip()
    
    #使用正則表達(dá)式獲取時間
#    result=findall(r'[0-9]{4}-[0-9]{2}-[0-9]{2}.+:[0-9]{2}',res.text)
    result=findall(r"(\d{4}-\d{1,2}-\d{1,2})",res.text)
    weixin_time=result[0]
    
    #獲取正文html并修改
    content=soup.find(id='js_content')
    soup2=BeautifulSoup((str(content)),"html.parser")
    soup2.div['style']='visibility: visible;'
    html=str(soup2)
    pattern=r'http[s]?:\/\/[a-z.A-Z_0-9\/\?=-_-]+'
    result = findall(pattern, html)
    
    #將data-src修改為src
    for url in result:
        html=html.replace('data-src="'+url+'"','src="'+url+'"')
    
    return html

#上傳圖片至服務(wù)器
def download_pic(content):
    
    pic_path= 'pic/' + str(path)+ '/'
    if not os.path.exists(pic_path):
        os.makedirs(pic_path)
        
    #使用正則表達(dá)式查找所有需要下載的圖片鏈接
    pattern=r'http[s]?:\/\/[a-z.A-Z_0-9\/\?=-_-]+'
    pic_list = findall(pattern, content)
    
    for index, item in enumerate(pic_list,1):
        count=1
        flag=True
        pic_url=str(item)
        
        while flag and count<=10:
            try:
                 data=requests.get(pic_url);
   
                 if pic_url.find('png')>0:
                     file_name = str(index)+'.png'
                     
                 elif pic_url.find('gif')>0:
                     file_name=str(index)+'.gif'
                     
                 else:
                     file_name=str(index)+'.jpg'

                 with open( pic_path + file_name,"wb") as f:
                     f.write(data.content)
                     
                 #將圖片鏈接替換為本地鏈接
                 content = content.replace(pic_url, pic_path + file_name)
                 
                 flag = False
                 print('已下載第' + str(index) +'張圖片.')
                 count += 1
                 time.sleep(1)
                      
            except:
                 count+=1
                 time.sleep(1)
                 
        if count>10:
            print("下載出錯:",pic_url)
    return content


def get_link(dir):
    link = []
    with open(dir,'r') as file_to_read:
        while True:
            line = file_to_read.readline()
            if not line:
                break
            line = line.strip('\n')
            link.append(line)
    return link

path = 'link.txt'
linklist = get_link(path)
print(linklist)
s = len(linklist)
        

if __name__ == "__main__":
    
    #獲取html
    input_flag=True
    while input_flag:
#        for j in range(0,s):
#            pic = str(j)
        j = 1
        for i in linklist:
            weixin_url = i  
            path = j
            j += 1     
            #weixin_url=input()
            re=findall(r'http[s]?:\/\/mp.weixin.qq.com\/s\/[0-9a-zA-Z_]+',weixin_url) 
            if len(re)<=0:
                    print("鏈接有誤,請重新輸入!")
            else:
                input_flag=False
            
            content=get_weixin_html(weixin_url)
            content=download_pic(content)
            #保存至本地
            with open(weixin_title+'.txt','w+',encoding="utf-8") as f:
                f.write(content) 
            with open(weixin_title+'.html','w+',encoding="utf-8") as f:
                f.write(content)  
                
            print()
            print("標(biāo)題:《"+weixin_title+"》")
            print("發(fā)布時間:"+weixin_time)

四、Python正則表達(dá)式匹配日期與時間

import re
from datetime import datetime

test_date = '小明的生日是2016-12-12 14:34,小張的生日是2016-12-21 11:34 .'
test_datetime = '小明的生日是2016-12-12 14:34,.小晴的生日是2016-12-21 11:34,好可愛的.'

# date
mat = re.search(r"(\d{4}-\d{1,2}-\d{1,2})",test_date)
print mat.groups()
# ('2016-12-12',)
print mat.group(0)
# 2016-12-12

date_all = re.findall(r"(\d{4}-\d{1,2}-\d{1,2})",test_date)
for item in date_all:
    print item
# 2016-12-12
# 2016-12-21

# datetime
mat = re.search(r"(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2})",test_datetime)
print mat.groups()
# ('2016-12-12 14:34',)
print mat.group(0)
# 2016-12-12 14:34

date_all = re.findall(r"(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2})",test_datetime)
for item in date_all:
    print item
# 2016-12-12 14:34
# 2016-12-21 11:34
## 有效時間

# 如這樣的日期2016-12-35也可以匹配到.測試如下.
test_err_date = '如這樣的日期2016-12-35也可以匹配到.測試如下.'
print re.search(r"(\d{4}-\d{1,2}-\d{1,2})",test_err_date).group(0)
# 2016-12-35

# 可以加個判斷
def validate(date_text):
    try:
        if date_text != datetime.strptime(date_text, "%Y-%m-%d").strftime('%Y-%m-%d'):
            raise ValueError
        return True
    except ValueError:
        # raise ValueError("錯誤是日期格式或日期,格式是年-月-日")
        return False

print validate(re.search(r"(\d{4}-\d{1,2}-\d{1,2})",test_err_date).group(0))
# false

# 其他格式匹配. 如2016-12-24與2016/12/24的日期格式.
date_reg_exp = re.compile('\d{4}[-/]\d{2}[-/]\d{2}')

test_str= """
     平安夜圣誕節(jié)2016-12-24的日子與去年2015/12/24的是有不同哦.
     """
# 根據(jù)正則查找所有日期并返回
matches_list=date_reg_exp.findall(test_str)

# 列出并打印匹配的日期
for match in matches_list:
  print match

# 2016-12-24
# 2015/12/24

以上就是Python實(shí)現(xiàn)快速保存微信公眾號文章中的圖片的詳細(xì)內(nèi)容,更多關(guān)于Python保存文章圖片的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集

    python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集

    這篇文章主要介紹了python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • python處理xls文件openpyxl基礎(chǔ)操作

    python處理xls文件openpyxl基礎(chǔ)操作

    這篇文章主要為大家介紹了python處理xls文件openpyxl基礎(chǔ)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Ruby元編程基礎(chǔ)學(xué)習(xí)筆記整理

    Ruby元編程基礎(chǔ)學(xué)習(xí)筆記整理

    元編程Metaprogramming是指編寫能夠操作其他程序的程序,例如C語言中的宏,是編程中的一項(xiàng)高級技巧,這里我們立足基礎(chǔ),來看一下Ruby元編程基礎(chǔ)學(xué)習(xí)筆記整理.
    2016-07-07
  • 使用celery和Django處理異步任務(wù)的流程分析

    使用celery和Django處理異步任務(wù)的流程分析

    Celery是 一個專注于實(shí)時處理的任務(wù)隊(duì)列,它還支持任務(wù)調(diào)度。 Celery快速,簡單,高度可用且靈活。這篇文章主要介紹了使用celery和Django處理異步任務(wù)的流程分析,需要的朋友可以參考下
    2020-02-02
  • python剪切視頻與合并視頻的實(shí)現(xiàn)

    python剪切視頻與合并視頻的實(shí)現(xiàn)

    這篇文章主要介紹了python剪切視頻與合并視頻的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • python中數(shù)據(jù)庫like模糊查詢方式

    python中數(shù)據(jù)庫like模糊查詢方式

    這篇文章主要介紹了python中數(shù)據(jù)庫like模糊查詢方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python繪制動態(tài)曲線教程

    python繪制動態(tài)曲線教程

    今天小編就為大家分享一篇python繪制動態(tài)曲線教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python OpenCV對本地視頻文件進(jìn)行分幀保存的實(shí)例

    Python OpenCV對本地視頻文件進(jìn)行分幀保存的實(shí)例

    今天小編就為大家分享一篇Python OpenCV對本地視頻文件進(jìn)行分幀保存的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python命令行解析器argparse詳解

    Python命令行解析器argparse詳解

    大家好,本篇文章主要講的是Python命令行解析器argparse詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • 讀取json格式為DataFrame(可轉(zhuǎn)為.csv)的實(shí)例講解

    讀取json格式為DataFrame(可轉(zhuǎn)為.csv)的實(shí)例講解

    今天小編就為大家分享一篇讀取json格式為DataFrame(可轉(zhuǎn)為.csv)的實(shí)例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論