I
I
ivan97272022-02-18 14:50:58
Python
ivan9727, 2022-02-18 14:50:58

Does the function not pass variables, or does it pass some of them?

Good afternoon, the first function works great, everything that is found on the page is parsed and added to the json file. But when you run the second function, it either doesn’t transfer anything and ends the script or adds 1 block, but it’s not even the last one that appeared, but some kind of old block. Please tell me what can be?

from email.mime import application
import json
from textwrap import indent
from selenium import webdriver
import config
import telebot
from telebot import types
import time
import requests
import re
import requests
from bs4 import BeautifulSoup as bs


def get_first_leads():
    leads = "n.php"
    url = config.URL + leads
    driver = webdriver.Firefox()
    driver.get(url)
    login = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/label/span/input')
    login.send_keys(config.LOGIN)
    time.sleep(0.1)
    passw = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/div/div/div[2]/label/span/input')
    passw.send_keys(config.PASSWORD)
    click = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/a')
    click.click()        
    time.sleep(15)
    page = driver.page_source
    soup = bs(page, 'lxml')
    applications_cards = soup.find_all(class_=re.compile("OrderSnippetContainerStyles__Container"))
    new_applcations = {}
    for application in applications_cards:
        if application.find('h3', class_=re.compile("SubjectAndPriceStyles")):
            application_title = application.find('h3', class_=re.compile("SubjectAndPriceStyles")).text.strip()
        else:
            application_title = "Нет заголовка"
        if application.find('p', class_=re.compile("SnippetBodyStyles__MainInfo")):
            application_snippet = application.find('p', class_=re.compile("SnippetBodyStyles__MainInfo")).text.strip()
        else:
            application_snippet = "Нет описания"
        if application.find('span', class_=re.compile("LocationAndScheduleStyles__TextContainer")):
            application_location = application.find('span', class_=re.compile("LocationAndScheduleStyles__TextContainer")).text.strip()
        else:
            application_location = "нет местоположения"
        if  application.find('span', class_=re.compile("LocationAndScheduleStyles__ScheduleText")):
            application_shedule = application.find('span', class_=re.compile("LocationAndScheduleStyles__ScheduleText")).text.strip()
        else:
            application_shedule= "Свободная дата"
            
        application_name_client = application.find('span', class_=re.compile("StatusAndClientInfoStyles__Name")).text.strip()
        application_link_req = application.find('a', class_=re.compile("SnippetBodyStyles__Container"))
        application_link = application_link_req.get('href')
        application_id = application_link_req.get('id')
        new_applcations [application_id] = {
                "application_title":application_title,
                "application_snippet":application_snippet,
                "application_location":application_location,
                "application_shedule":application_shedule,
                "application_name_client":application_name_client,
                "application_link":url + application_link,
                "application_id":application_id

        }
        with open ("new_applications.json", "w") as file:
            json.dump(new_applcations, file, indent =4, ensure_ascii=False)

def check_application_updater ():
    with open ("new_applications.json") as file:
        new_applications= json.load(file)
    leads = "n.php"
    url = config.URL + leads
    driver = webdriver.Firefox()
    driver.get(url)
    login = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/label/span/input')
    login.send_keys(config.LOGIN)
    time.sleep(0.1)
    passw = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/div/div/div[2]/label/span/input')
    passw.send_keys(config.PASSWORD)
    click = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/a')
    click.click()        
    time.sleep(15)
    page = driver.page_source
    soup = bs(page, 'html.parser')
    
    applications_cards = soup.find_all(class_=re.compile("OrderSnippetContainerStyles__Container"))
    fresh_applications = {}
    while True:
        for application in applications_cards:
            application_link_req = application.find('a', class_=re.compile("SnippetBodyStyles__Container"))
            application_link = application_link_req.get('href')
            application_id = application_link_req.get('id')
            if application_id in new_applications:
                continue
            else: 
                for application in applications_cards:
                    if application.find('h3', class_=re.compile("SubjectAndPriceStyles")):
                        application_title = application.find('h3', class_=re.compile("SubjectAndPriceStyles")).text.strip()
                    else:
                        application_title = "Нет заголовка"
                    if application.find('p', class_=re.compile("SnippetBodyStyles__MainInfo")):
                        application_snippet = application.find('p', class_=re.compile("SnippetBodyStyles__MainInfo")).text.strip()
                    else:
                        application_snippet = "Нет описания"
                    if application.find('span', class_=re.compile("SnippetBodyStyles__MainInfo")):
                        application_location = application.find('span', class_=re.compile("SnippetBodyStyles__MainInfo")).text.strip()
                    else:
                        application_location = "нет местоположения"
                    if  application.find('span', class_=re.compile("LocationAndScheduleStyles__ScheduleText")):
                        application_shedule = application.find('span', class_=re.compile("LocationAndScheduleStyles__ScheduleText")).text.strip()
                    else:
                        application_shedule= "Свободная дата"
                            
                        application_name_client = application.find('span', class_=re.compile("StatusAndClientInfoStyles__Name")).text.strip()
                        # application_link_req = application.find('a', class_=re.compile("SnippetBodyStyles__Container"))
                        # application_link = application_link_req.get('href')
                        # application_id = application_link_req.get('id')
                        new_applications [application_id] = {
                            "application_title":application_title,
                            "application_snippet":application_snippet,
                            "application_location":application_location,
                            "application_shedule":application_shedule,
                            "application_name_client":application_name_client,
                            "application_link":url + application_link,
                            "application_id":application_id
                        }
                        fresh_applications [application_id] = {
                            "application_title":application_title,
                            "application_snippet":application_snippet,
                            "application_location":application_location,
                            "application_shedule":application_shedule,
                            "application_name_client":application_name_client,
                            "application_link":url + application_link,
                            "application_id":application_id
                        }
                        
                        with open ("new_applications.json", "w") as file:
                            json.dump(new_applications, file, indent =4, ensure_ascii=False)

                        return fresh_applications
        time.sleep(60)
def main():
    #get_first_leads()
    print(check_application_updater())
if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question