Y
Y
Ynkersss2021-01-25 17:21:15
Python
Ynkersss, 2021-01-25 17:21:15

Why does the parser in the loop not update the result?

I wanted to make a VK bot that would drop updating information from the site when a certain command was typed, as a result, there was a problem that the parser in the loop does not update the result, but the same information remains , so I created my own site so as not to wait for the right one to be updated and threw a separate script that concentrates on exactly this problem (which keeps the same problem).

In short, the code gives the console a link to the image on the site 2 times, with an interval of 10 seconds, so that I can change the html script)

import time

import requests
from bs4 import BeautifulSoup

URL = 'http://s336444.smrtp.ru/'
HOST = 'http://s336444.smrtp.ru/'
HEADERS = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
}

#Парсер
def get_html(url, params=None):
    r = requests.get(url, headers=HEADERS, params=params)
    return r

def get_content(html):
    soup = BeautifulSoup(html, 'html.parser')
    p = soup.find_all('div')
    img = []

    for imgs in p:
        img.append((imgs.find('img').get('src')))

    forest = img[0]

    return forest

html = get_html(URL)
output = get_content(html.text)

#Цикл, для вывода в консоль URL изображения
for i in range(2):
    output
    print('\n' + output + ' ' + str(i))
    if i == 1:
        break
    time.sleep(10)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-01-25
@Ynkersss

the parser in the loop does not update the result, but the same information remains

In what cycle? In ? There is not a single assignment / any other modification in it. It just prints textfor i in range(2):
for i in range(2):
    html = get_html(URL)
    output = get_content(html.text)
    print('\n' + output + ' ' + str(i))
    if i == 1:
        break
    time.sleep(10)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question