Python零基础爬虫速成②:批量爬取微信公众号图片(基于beautifulsoup爬取吉他谱)

支持库安装

pip install request
pip install beautifulsoup4
pip install Pillow

Requests :唯一的一个非转基因的 Python HTTP 库,人类可以安全享用。

Beautiful Soup :是一个可以从HTML或XML文件中提取数据的Python库。它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式。

Pillow 是一个对 PIL 友好的分支,而 PIL 是一个 Python 图像处理库。

网页分析

网页地址:
https://mp.weixin.qq.com/s/RQKeWQuOgtCnml-prMXC5w
在这里插入图片描述

爬取步骤:

  • 爬取文章列表
  • 文章页面发请求
  • 爬取图片列表
  • 保存图片
    在这里插入图片描述

代码部分分析

文章列表爬取

find_all方法参数:
find_all( name , attrs , recursive , string , **kwargs )

参数名描述
name标签名,如“a”筛选<a>标签
attrs参数字典
recursive是否递归?默认真,遍历所有子节点
limit返回一定数目的节点
Stringstring 参数接受 字符串 , 正则表达式 , 列表, True
**kwargs过滤器

**返回值:**节点集合ResultSet
详细信息:
https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/#id27

def get_article_list(web_url):
    ans = {}
    html = requests.get(web_url).text
    soup = BeautifulSoup(html, 'html.parser')
    result_set = soup.find_all('section', attrs={
        'style': 'padding-right: 0.6em;padding-bottom: 1em;padding-left: 0.6em;box-sizing: border-box;'})
    for result in result_set:
        category = result.find('span').text #不同类别
        sub_result_set = result.find_all('a')
        article_list=[]
        for sub_result in sub_result_set:
            article_url = sub_result['href']
            song_name = sub_result.text
            song_name = song_name.split('》')[0].replace('《','')
            article_list.append({'name':song_name,'url':article_url})
        if len(article_list)!=0: ans[category] = article_list

    return ans

图片列表爬取

find_all(使用了匿名函数布尔表达式)
不是很容易判断图片位置(微信网页结构太乱)

def get_img_url(web_url):
    html = requests.get(web_url).text
    soup = BeautifulSoup(html, 'html.parser')
    imgs = soup.find_all(lambda x:x.has_attr('data-src') and x.name=='img' and x['data-w']!='' and int(x['data-w'])>1000)
    imgs_url_list = [imgs[i]['data-src'] for i in range(len(imgs))]
    return imgs_url_list

图片保存

根据图片的url判断图片的尺寸
再决定是否保存

def write_img(file_path,img):
    with open(file_path, 'wb') as fd:
        fd.write(img)
def write_img_from_list(img_url_list,song_name):
    num=0
    for idx,img_url in enumerate(img_url_list,1):
        img = requests.get(img_url).content
        im = Image.open(BytesIO(img))
        if im.width < im.height:
            num += 1
            file_path = os.path.join(key, song_name + '_' + str(num) + '.jpg', )
            write_img(file_path,img)
    print(song_name+':已爬取' + str(num) + '张图:')

未来工作

  • 模拟添加cookie和header(暂时不需要 )
  • 加快速度(并行多任务处理)

下载

开源地址:
https://github.com/ESwordCn/crawler_wechat_subscriptions

打包下载:

已按照不同首字母排序
上千张图片
下载地址:
https://download.csdn.net/download/Ejzq1/85065181

想免费下载?
下方点赞+关注+评论

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赛文X

觉得不错就打赏下呗mua~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值