S
S
SerjLore2020-09-06 16:30:30
Python
SerjLore, 2020-09-06 16:30:30

Parsing more than 1 page?

Hello.
Is there a way to scrape more than 1 page of photos on imgur?

Also the code:

from bs4 import BeautifulSoup as bs
import requests

def save():
  with open('C:/konosuba_par.txt', 'a') as file:
    file.write(f'>Link: {comp["image"]}\n')

def parse():
  URL = 'https://imgur.com/search/score?q=konosuba'
  HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.41'}

  responce = requests.get(URL, headers = HEADERS)
  soup = bs(responce.content, 'html.parser')
  items = soup.findAll('div', class_ = 'post')
  comps = []

  for item in items:
    comps.append({
      'post': item.find('a', class_ = 'image-list-link').get_text(strip = True),
      'image': item.find('img').get('src')
      })

  global comp
  for comp in comps:
    print(f'Link: {comp["image"]}\n')
    save()

parse()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexBoss, 2020-09-06
@SerjLore

There is a way, depending on your skills and desires.
1) the library is good through concurrent.future streams
2) rewrite everything on asynchronous
If a similar question arises, it is better to use the first option

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question