forked from WilhelmvonArndt/scraper-blocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
65 lines (50 loc) · 1.74 KB
/
scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
from bs4 import BeautifulSoup
import time
import random
import yagmail
URL = "https://www.blocket.se/annonser/hela_sverige"
def parse_site(url):
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
return soup
def send_email(header, info):
receiver = "[email protected]"
yag = yagmail.SMTP("[email protected]")
yag.send(
to=receiver,
subject=header,
contents=info,
)
def get_latest_add():
soup = parse_site(URL)
ad_elems = soup.find_all("article", class_="hidZFy")
latest_add = ad_elems[0]
ad_name = latest_add.find("span", class_="jzzuDW").text
ad_price = latest_add.find("div", class_="jkvRCw").text
ad_link = latest_add.find("a", class_="enigRj")["href"]
return (ad_name, ad_price, ad_link)
get_latest_add()
prev_ad_name, prev_ad_price, prev_ad_link = get_latest_add()
print("The latest ad is: ", prev_ad_name, "\nPrice: ", prev_ad_price, "\nLink: ", prev_ad_link, sep='')
found_ad_counter = 0
while True:
duration_sleep = random.randint(50,60)
t = time.localtime()
current_time = time.strftime("%H:%M:%S",t)
time.sleep(duration_sleep)
ad_name, ad_price, ad_link = get_latest_add()
if ad_name == prev_ad_name:
#No new ads
print("No new ads..." + current_time)
pass
else:
#Match found
ad_link = "https://www.blocket.se" + ad_link
ad_link = ad_link.strip()
email_contant = "The latest ad is: " + ad_name, "\nPrice: " + ad_price + "\nLink: ", ad_link,
print("Ny annons: " + ad_name + current_time)
print("sending email...")
send_email(ad_name, email_content)
prev_ad_name = ad_name
found_ad_counter += 1