-2

I have made a script using beautifulsoup in order to get a price from a website. As a whole it works perfectly, cause it gives me the online price. But the problem is that :

  1. when I'm choosing another lg which is asked first it doesn't pay attention to the USER input and just works as it wishes to... now I want to know what is wrong with this, and another important thing is that:
  2. How can I Save the price with date in a text or CSV file...
  3. how can I re-run the app with a request of the user?

Here is my code:

 #<div class="c-product__seller-price-raw js-price-value">۴,۵۹۵,۰۰۰ </div>

import requests
from bs4 import BeautifulSoup
html="https://www.digikala.com/product/dkp-1800630/%DA%AF%D9%88%D8%B4%DB%8C-%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84-%D8%B3%D8%A7%D9%85%D8%B3%D9%88%D9%86%DA%AF-%D9%85%D8%AF%D9%84-galaxy-a70-sm-a705fnds-%D8%AF%D9%88-%D8%B3%DB%8C%D9%85%DA%A9%D8%A7%D8%B1%D8%AA-%D8%B8%D8%B1%D9%81%DB%8C%D8%AA-128-%DA%AF%DB%8C%DA%AF%D8%A7%D8%A8%D8%A7%DB%8C%D8%AA"

user=input("Please select your language, English 'E' or Persian 'P': ")

def Persian_App():
    request= requests.get(html)
    content=request.content
    soup=BeautifulSoup(content,"html.parser")
    element=soup.find("div",{"class":"c-product__seller-price-raw js-price-value"})
    print(" Gheymate Gooshie A70 Dar hale Hazer: ")
    print(element.text.strip())
    print("Anjam Shod!")
    return ("")

def English_App():
    request= requests.get(html)
    content=request.content
    soup=BeautifulSoup(content,"html.parser")
    element=soup.find("div",{"class":"c-product__seller-price-raw js-price-value"})
    print(" The Price of Samsung A70 Serie at the moment is:  ")
    print(element.text.strip())
    print("Done")
    return ("")

if user == "E" or "e":
    print(English_App())    
elif user == "P"or"p":
    print(Persian_App())
else:
    print("Please Enter a correct letter!")
    pass
print(input("Press ANY Key to Exit!"))
khelwood
  • 55,782
  • 14
  • 81
  • 108
Ata
  • 33
  • 1
  • 11

1 Answers1

0

Question 1

if user == "E" or "e":

is equivalent in python to:

if (user == "E") or "e":

and "e" evaluates to True so even if user is different from "E", the first condition will always be True and your code will always execute English_App.

To fix your problem:

if user in ("E"; "e"):

or

if user.lower() ==  "e":

Question 2

You need to save the result of your function into a variable like:

result = None
if user.lower() == "e":
    result = English_App()
elif user.lower() == "p":
    result  = Persian_App()
else:
    result = "Please Enter a correct letter!"

then you can use the csv module to save the result into a file.

Question 3

You have several possibilities for that. Check for instance:

Continuos loop with user input condition in python?

stellasia
  • 5,372
  • 4
  • 23
  • 43
  • It worked Perfectly my friend, thanks for your help. I got my answers... thanks a million. – Ata Dec 29 '19 at 21:12