Web Scrapping And Mailbot Using Python

2 minute read

Web Scrapping And Mailbot Using Python For Amazon/Flipkart India

A program which checks for price fall below a range and automatically sends mail with link!(My First .py Project)

Note : This app runs for pricing from Rs.100 (hope it works for lakhs)

Download the code from Github page : Click Here

This program was written with reference from: Click Here

Requirements:

  1. Python 3: Click Here
  2. Packages: Copy paste the below line to install the packages in one go using cmd (Inside the python app directory)

pip install requests stdiomask bs4 colorama

Packages used:

  • requests : Allows you to send HTTP requests
  • stdiomask : To mask the password
  • bs4 : To scrap from Website
  • colorama : For colors (just for fun)
  • smtplib : To establish connection and login e-mail
  • time : To create delay

Inputs to be given by the user:

  1. URL of the website
  2. User’s mail address
  3. User’s Password
  4. Destination mail address
  5. Your Budget

Note for Password:(For better Protection)

  • Enable Two step Authentication
  • Goto google app passwords
  • Get 16 digit Password Code
  • Enter in the input Password

If Lazy: (Less Protection)

  • Enable Google Less Secure Apps
  • Enter Your Default Password

Some Code Explained

Check and change for your UserAgent (just google and paste in the code)

	headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}

Colors

  • Refer to colorama pypi to learn about the package: Click Here

Getting Web Page Info

  • Requests is a package in python which helps in http connections.
  • BeautifulSoup helps in getting the html code from the website.
      URL = input('Enter URL: ')
      headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}
      page =  requests.get(URL, headers=headers) #Allows you to send HTTP requests
      soup = BeautifulSoup(page.content, 'html.parser') #Getting websites's content
    

Getting Product Name and Price:

  • Find() is used to get the data by entering its variable name.
  • We have to give rhe id/attributes to find the variable name.
      title = soup.find('span', attrs={'class':'_35KyD6'}).get_text()
      price = soup.find('div', attrs={'class':'_1vC4OE _3qQ9m1'}).get_text()
    

Go-to amazon/flipkart product page, inspect(f12) and click on the up-left button, keep the cursor on the price & name to get its id/attributes.

SMTP Library

  • SMTP means Simple Mail Transfer Protocol which helps in sending mail
      server = smtplib.SMTP('smtp.gmail.com',587) #port 587
      server.ehlo() # identify with the server
      server.starttls() #secure protocol establishment
      server.ehlo() 
      server.login(login_id,password) #login
      server.sendmail(login_id,dest_mail,msg) #sennding mail
      server.quit()
    

Time:

  • Sleep function delays the program in seconds.
      time.sleep(50)
    

Enjoy Buying Stuff!!