Jump to content
Sign in to follow this  
Bruno amoedo

Error importing a library

Recommended Posts

So, i'm trying to run this python code but without sucess can anyone helpe me ? 

only the bs4 library of the problem

this is python code:

Capturarxxxx.thumb.PNG.bed11c285bbcb869f4f9bc75d0d1124e.PNG

 

import bs4
import requests
import pandas as pd
def main():
    proxies={
        "http": "",
        "https": ""
    }
    keyword = input('\nKeyword: ')
    zipCode = input('\nZip Code: ')
    name = []
    barCode = []
    price = []
    issue = []
    address = []
    distance = []
    session = requests.Session()
    r = session.get('https://precodahora.ba.gov.br/produtos/', proxies=proxies)
    cookie = session.cookies.get_dict()
    soup = bs4.BeautifulSoup(r.text, features='lxml')
    csrf_token = soup.select_one('meta[id="validate"]')['data-id']
    headers = {
        'X-CSRFToken': csrf_token,
        'X-Requested-With': 'XMLHttpRequest'
    }
    locPayload = {
        'cep': zipCode
    }
    try:
        locRes = session.post('https://precodahora.ba.gov.br/geolocation/', proxies=proxies, headers=headers, cookies=cookie, data=locPayload).json()
        if locRes['codigo'] == 80:
            lat = locRes['lat']
            lon = locRes['lon']
            pagina = 1
            while True:
                proPayload = {
                    'termo': keyword,
                    'horas': 24,
                    'latitude': lat,
                    'longitude': lon,
                    'raio': 1,
                    'pagina': pagina,
                    'ordenar': 'preco.desc',
                }
                proRes = session.post('https://precodahora.ba.gov.br/produtos/', proxies=proxies, headers=headers, cookies=cookie, data=proPayload).json()
                if proRes['codigo'] == 80:
                    products = proRes['resultado']
                    for i, product in enumerate(products):
                        name.append(product['produto']['descricao'])
                        barCode.append(product['produto']['gtin'])
                        price.append(product['produto']['precoBruto'])
                        issue.append(product['produto']['data'])
                        address.append(product['estabelecimento']['endLogradouro'] + ' ' + product['estabelecimento']['endNumero'] + ' ' + product['estabelecimento']['bairro'] + ' ' + product['estabelecimento']['cep'] + ', ' + product['estabelecimento']['municipio'])
                        distance.append(product['estabelecimento']['distancia'])
                    pagina += 1
                else:
                    if pagina == 1:
                        print(proRes['descricao'])
                    break
            if pagina > 1:
                dict = {'Product Name': name,'Price': price,'Nome do estabelecimento':address,'distancia':distance}
                df = pd.DataFrame(dict)
                print("\n", df)
                print("\n")
                #df.to_csv(keyword+'.csv', encoding='utf-8-sig')
        else:
            print(locRes['descricao'])
    except:
        print('\nPlease enter your zip code correctly (*****-***)')

if __name__ == '__main__':
    main()

Edited by Bruno amoedo

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×