欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

爬取http://www.doutula.com网站表情包

程序员文章站 2022-03-26 14:37:01
...

import requests
from bs4 import BeautifulSoup

def main(n):
for i in range(n):
url = f’http://www.doutula.com/photo/list/?page={i}
response = requests.get(url=url)

    soup = BeautifulSoup(response.text, 'html.parser')

    div = soup.find(name='div', attrs={"class": 'page-content text-center'})

    a_list = div.find_all(name="a")

    for a in a_list:
        img = a.find('img')
        img_name = img.get('alt')
        img_url = img.get('data-backup')
        # print(img_name, img_url)

        if img_name and img_url:
            try:
                with open('表情包/%s.jpg' % img_name, 'wb')as f:
                    ret_img = requests.get(url=img_url)
                    f.write(ret_img.content)
            except Exception:
                continue

main(5)