Рубрики
Без рубрики

кража некоторых мемов с помощью Python

Привет: D. Я люблю мемы и хочу держать их в своем телефоне, я думаю, что решение состоит в том, чтобы просмотреть THR … Tagged с помощью Python, Scraping, HTML, мемов.

Привет: D.

Я люблю мемы и хочу держать их в своем телефоне, я думаю, что решение состоит в том, чтобы просмотреть мем и загрузить его вручную?

Нет, давайте украсть/загрузите Memes Automatic с Python, какой веб -сайт мы украдим мемы из него 🎯?

Наша цель – https://imgflip.com/

Сначала давайте посмотрим на HTML -страницу

BLA BLAB BLAB LBA BL BLA

Все мемы ссылки в

.......

Здесь нам нужно проанализировать тег div с BASE-IMG-WRAP Имя класса и получить метка в этом дивизии

BLA BLA LBA

Модули, которые нам нужны

  • Запросы (для запросов http/s)
  • BS4 (HTML SAING)

Давайте начнем нашу работу с отправки HTTP -запроса на этот сайт и анализ BASE-IMG-WRAP класс

import requests
from bs4 import BeautifulSoup

req = requests.get('https://imgflip.com/?page=1').content
soup = BeautifulSoup(req, "html.parser")
ancher = soup.find_all('div', {'class': "base-unit clearfix"})

"""

Мы взяли все данные

Давайте получим метку IMG

import requests
from bs4 import BeautifulSoup

r = requests.get('https://imgflip.com/?page=1').content
soup = BeautifulSoup(req, "html.parser")
ancher = soup.find_all('div', {'class': "base-unit clearfix"})

for pt in ancher:
    img = pt.find('img', {'class': 'base-img'})
    if img:
        print(img)
 Why is my sister's name Rose |  people that upvote good memes instead of just scrolling past them | image tagged in why is my sister's name rose | made w/ Imgflip meme maker
Petition: upvote if you want a rule against upvote begging. I will then post the results in the Imgflip suggestion stream |  Upvote begging will keep happening as long as they make it to the front page; UPVOTE BEGGING TO DESTROY UPVOTE BEGGING | image tagged in memes,the scroll of truth,no no hes got a point,you have become the very thing you swore to destroy,memes | made w/ Imgflip meme maker

круто, знай, что у нас есть все тег IMG Знаю, нам нужно получить Значение SRC

import requests
from bs4 import BeautifulSoup

r = requests.get('https://imgflip.com/?page=1').content
soup = BeautifulSoup(req, "html.parser")
ancher = soup.find_all('div', {'class': "base-unit clearfix"})

for pt in ancher:
    img = pt.find('img', {'class': 'base-img'})
    if img:
        link = img['src'].replace(img['src'][0:2],'https://')
        print(link)

"""
https://i.imgflip.com/5aq7jq.jpg
https://i.imgflip.com/5aqvx4.jpg
https://i.imgflip.com/5aq5jg.jpg
https://i.imgflip.com/5aor2n.jpg
https://i.imgflip.com/5amt83.jpg
https://i.imgflip.com/5ayodd.jpg
https://i.imgflip.com/5awhgz.jpg
https://i.imgflip.com/5allij.jpg
https://i.imgflip.com/5aosh7.jpg
https://i.imgflip.com/5amxbo.jpg
https://i.imgflip.com/5auvpo.jpg
"""

После получения всех изображений мы скачаем его с модулем запросов и сохраним

import requests
from bs4 import BeautifulSoup

req = requests.get('https://imgflip.com/?page=1').content
soup = BeautifulSoup(req, "html.parser")
ancher = soup.find_all('div', {'class': "base-unit clearfix"})

for pt in ancher:
    img = pt.find('img', {'class': 'base-img'})
    if img:
        link = img['src'].replace(img['src'][0:2],'https://')
        r = requests.get(link)
        f = open(img['src'].split('/')[3],'wb') # write binary
        f.write(r.content)
        f.close()

Отлично, мы получаем все мемы номера страницы 1 Давайте добавим параметр для Страница в URL

import requests
from bs4 import BeautifulSoup
def meme_stealer(page):
    req = requests.get(f'https://imgflip.com/?page={page}').content
    soup = BeautifulSoup(req, "html.parser")
    ancher = soup.find_all('div', {'class': "base-unit clearfix"})
    for pt in ancher:
        img = pt.find('img', {'class': 'base-img'})
        if img:
            link = img['src'].replace(img['src'][0:2],'https://')
            r = requests.get(link)
            f = open(img['src'].split('/')[3],'wb')
            f.write(r.content)
            f.close()

for i in range(1,6):
    meme_stealer(i)
# Page 1
# Page 2
# Page 3
# Page 4
# Page 5

Спасибо, что прочитали это пока: D

Оригинал: “https://dev.to/knassar702/steal-some-memes-with-python-lpe”