Here is a simple scraper (with added history saving) that searches the website provided with a link for a element/list of elements with a specific class or id.
- Enter the link to website you would like to scrape in the variable URL like this
URL="https://www.website.com"(line 17) - Enter the keywords you would like the scraper to search for like this
keywords=["keyword1", "keyword2", "keyword3"](line 14) - Change the "classname" in
results = soup.find_all("div", {"class": "classname"})to the name of the class you are looking for (if you are looking for an id, then change "class" to "id" and "classname" to the id name you are looking for) - The lines 65 to 75 are looking for a child element "a", extracting the "href" and shortening the link to tinyurl. You can change it to something else if you would like to do something.
- Keep in mind to have variable
foundset to "False" before checking for results and if you find one change it to false, to alert via mail. - Also add each newly found element in the
temp_dictionaryanddictionaryso the program is able to send email with only new results and store them with others for the future. - Also set all the variables in the
alertViaEmail()function to your info, so it can send an email correctly. - The
smtp_email="smtp.gmail.com"andsmtp_port=587are already set for gmail's smtp server, please change them if you are using a different provider according to their instructions. - Variable
valueforauthis storing the password for your email. When using gmail you need to generate a so called "APP PASSWORD" in your GOOGLE ACCOUNT settings (Security/2-Step-Verification/App Passwords, In a dropdown menu chose other and generate. Copy the generated code tovalueforauth). - You can also add more than one email address to
receiver_email, just keep them in the same string and seperate them with a "," like thisreceiver_email="username@email.com, username2@email.com". - Enjoy!
