微信公众号文章图片 / 壁纸下载

查看 128|回复 10
作者:Listentomusic   
简单的公众号图片壁纸下载;
import re
import requests
url = 'https://mp.weixin.qq.com/s/E0TTnkG0-kWetdf49edh9g'
headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/124.0.0.0 Safari/537.36"
}
response = requests.get(url=url, headers=headers).text
rule = re.compile('data-src="([^"]+)"').findall(response)
i = 1
for line in rule:
    # 过滤掉非链接的数据
    if line.startswith('https') or line.startswith('http'):
        images_data = requests.get(url=line, headers=headers).content
        with open('./壁纸/' + str(i) + '.jpg', 'wb') as f:
            f.write(images_data)
        print('正在下载第: ' + str(i) + '张图片')
        i += 1
print('壁纸全部下载完成, 请注意查看!')

图片, 公众

wkdxz   

效果不错,我测试时会报路径错误和https错误,小改了下。
[Python] 纯文本查看 复制代码# -*- coding: utf-8 -*-
import re
import requests
import os
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
url = "https://mp.weixin.qq.com/s/E0TTnkG0-kWetdf49edh9g"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/124.0.0.0 Safari/537.36"
}
response = requests.get(url=url, headers=headers).text
rule = re.compile('data-src="([^"]+)"').findall(response)
i = 1
img_path = "E:/壁纸"
if not os.path.exists(img_path):
    os.makedirs(img_path)
for line in rule:
    # 过滤掉非链接的数据
    if line.startswith(("https", "http")):
        images_data = requests.get(url=line, headers=headers).content
        with open(f"{img_path}/{str(i)}.jpg", "wb") as f:
            f.write(images_data)
        print("正在下载第 " + str(i) + " 张图片")
        i += 1
print("壁纸全部下载至【" + img_path + "】文件夹下, 请注意查看!")
happyxuexi   

也修改了一下,把url作为参数传入。这样会更方便点。
比如:python downmp.py https://mp.weixin.qq.com/s/E0TTnkG0-kWetdf49edh9g
另外文件夹是url的/最后一段。
谢谢楼主大佬还有我楼上的大佬们。
[Python] 纯文本查看 复制代码# -*- coding: utf-8 -*-
import re
import requests
import os
import urllib3
import sys

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
print ('参数个数为:', len(sys.argv), '个参数。')
url = sys.argv[1]
print(url)
#url = "https://mp.weixin.qq.com/s/8iRaD1YVk2PGKfh-pszdYw"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/124.0.0.0 Safari/537.36"
}

response = requests.get(url=url, headers=headers).text
rule = re.compile('data-src="([^"]+)"').findall(response)
i = 1

start = url.rindex("/")
#find和index函数可以正向查找,并返回首次出现该字符的位置。而rfind和rindex则是从末尾往前查找。
name = url[start+1:]
img_path = "E:/壁纸/"+name
if not os.path.exists(img_path):
    os.makedirs(img_path)

for line in rule:
    # 过滤掉非链接的数据
    if line.startswith(("https", "http")):
        images_data = requests.get(url=line, headers=headers).content
        with open(f"{img_path}/{str(i)}.jpg", "wb") as f:
            f.write(images_data)
        print("正在下载第 " + str(i) + " 张图片")
        i += 1

print("壁纸全部下载至【" + img_path + "】文件夹下, 请注意查看!")
wapjsx   

感觉很不错~~加油!
Listentomusic
OP
  


wkdxz 发表于 2024-5-21 15:59
效果不错,我测试时会报路径错误和https错误,小改了下。
[mw_shl_code=python,true]# -*- coding: utf- ...

学习了,刚学的爬虫不太会学
hackerSQL   

正好需要
anorith   


wkdxz 发表于 2024-5-21 15:59
效果不错,我测试时会报路径错误和https错误,小改了下。
[mw_shl_code=python,true]# -*- coding: utf- ...

这个很实用,代码也相当简短
炎爝   

感谢分享 !!!!
52soft   

这个工具不错
liangqz   

感谢分享
您需要登录后才可以回帖 登录 | 立即注册

返回顶部