-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2dehandstalker.py
executable file
·60 lines (53 loc) · 1.92 KB
/
2dehandstalker.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
#!/bin/env python
import argparse
import json
import requests
from bs4 import BeautifulSoup
def get_parsed_arguments():
parser = argparse.ArgumentParser(description="Get 2dehands items")
parser.add_argument("--filter", help="optional search filter")
parser.add_argument(
"--pages", help="optional amount of pages (default: 1)", default=1
)
return parser.parse_args()
def get_2dehands_items(query, pages):
results = []
for page in range(0, int(pages)):
offset = 38 * page
page = requests.get(
f"https://www.2dehands.be/markt/2/${query}/?offset=${offset}"
)
soup = BeautifulSoup(page.text, "html.parser")
if soup.find(class_="panel has-icon no-results"):
return None
elif soup.find(class_="search-result"):
items = soup.find_all("article")
for item in items:
item_json = {
"name": item.find(class_="listed-adv-item-link")
.contents[0]
.strip(),
"date": item.find(class_="listed-item-date").contents[0].strip(),
"price": item.find(class_="listed-item-price").contents[0].strip(),
"url": item.find(class_="listed-adv-item-link").get("href").strip(),
"image": item.find(class_="item-center-image").get("src").strip(),
}
if item_json:
results.append(item_json)
else:
print(
"Something went really wrong here: we found nothing, really, as in not a single thing...."
)
break
offset += 38
return results
if __name__ == "__main__":
ARGS = get_parsed_arguments()
print(
json.dumps(
get_2dehands_items(ARGS.filter, ARGS.pages),
sort_keys=True,
indent=4,
ensure_ascii=False,
)
)