Python 抓取微信文章图片

WechatIMG194.jpeg

Python 抓取微信文章图片

  • 创建 crawl_image.py,代码如下。
# -*- coding: utf-8 -*-
# !python3
import re
import os
import ssl
import requests
from bs4 import BeautifulSoup

def getHTMLText(url):
    '''
    Get page information
    '''
    try:
        r=requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return ""

def getimgURL(html):
    '''
    Parse the webpage and get all the image urls
    '''
    soup = BeautifulSoup(html , "html.parser")
    adlist = []
    for i in soup.find_all("img"):
        try:
            ad = re.findall(r'.*src="(.*?)?" .*',str(i))
            if ad :
                adlist.append(ad)
        except:
            continue
    del adlist[-1]
    return adlist

#
def download(adlist):
    '''
    Create a new folder pic,download and 
    save the crawled picture information
    '''
    root = "pic/"
    for i in range(len(adlist)):
        path = root+str(i)+"."+'png'
        if not os.path.exists(root):
            os.mkdir(root)
        if not os.path.exists(path):
            r = requests.get(adlist[i][0])
            with open(path, 'wb') as f:
                f.write(r.content)
                f.close()

ssl._create_default_https_context = ssl._create_unverified_context
url = 'https://mp.weixin.qq.com/s/5A8ooI15XtS6FR3yo4AHpg'
html = getHTMLText(url)
list = getimgURL(html)
download(list)
  • 执行
$ python3 crawl_image.py
  • 查看 pic 目录
$ ls pic
0.png 1.png 2.png 3.png 4.png 5.png 6.png 7.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值