-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtest.py
154 lines (137 loc) · 5.49 KB
/
test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import json
from pathlib import Path
from cerberus import Validator
import idealista
import pytest
import pprint
import os
from scrapfly import ScrapeConfig, ScrapflyClient
SCRAPFLY = ScrapflyClient(key=os.environ["SCRAPFLY_KEY"])
BASE_CONFIG = {
# bypass web scraping blocking
"asp": True,
# set the proxy country to Spain
"country": "ES",
"cache": True
}
pp = pprint.PrettyPrinter(indent=4)
class Validator(Validator):
def _validate_min_presence(self, min_presence, field, value):
pass # required for adding non-standard keys to schema
def require_min_presence(items, key, min_perc=0.1):
"""check whether dataset contains items with some amount of non-null values for a given key"""
count = sum(1 for item in items if item.get(key))
if count < len(items) * min_perc:
pytest.fail(
f'inadequate presence of "{key}" field in dataset, only {count} out of {len(items)} items have it (expected {min_perc*100}%)'
)
def validate_or_fail(item, validator):
if not validator.validate(item):
pp.pformat(item)
pytest.fail(f"Validation failed for item: {pp.pformat(item)}\nErrors: {validator.errors}")
property_schema = {
"schema": {
"type": "dict",
"schema": {
"url": {"type": "string"},
"title": {"type": "string"},
"location": {"type": "string"},
"currency": {"type": "string"},
"price": {"type": "integer"},
"description": {"type": "string"},
"updated": {"type": "string"},
"features": {
"type": "dict",
"schema": {
"Basic features": {"type": "list", "schema": {"type": "string"}},
"Amenities": {"type": "list", "schema": {"type": "string"}},
"Energy performance certificate": {
"type": "list",
"schema": {"type": "string"},
},
},
},
"images": {
"type": "dict",
"schema": {
"Living room": {"type": "list", "schema": {"type": "string"}},
"Kitchen": {"type": "list", "schema": {"type": "string"}},
"Bathroom": {"type": "list", "schema": {"type": "string"}},
"Bedroom": {"type": "list", "schema": {"type": "string"}},
},
},
"plans": {"type": "list", "schema": {"type": "string"}},
},
}
}
search_schema = {
"title": {"type": "string"},
"link": {"type": "string"},
"picture": {"type": "string"},
"price": {"type": "integer"},
"currency": {"type": "string"},
"parking_included": {"type": "boolean"},
"details": {"type": "list", "schema": {"type": "string"}},
"description": {"type": "string"},
"tags": {"type": "list", "schema": {"type": "string"}},
"listing_company": {"type": "string", "nullable": True},
"listing_company_url": {"type": "string", "nullable": True}
}
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_idealista_scraping():
first_page = await SCRAPFLY.async_scrape(
ScrapeConfig(
url="https://www.idealista.com/en/venta-viviendas/marbella-malaga/con-chalets/",
asp=True,
country="ES",
)
)
property_urls = idealista.parse_search(first_page)
to_scrape = [
ScrapeConfig(first_page.context["url"] + f"pagina-{page}.htm", asp=True, country="ES") for page in range(2, 3)
]
async for response in SCRAPFLY.concurrent_scrape(to_scrape):
property_urls.extend(idealista.parse_search(response))
result = await idealista.scrape_properties(urls=property_urls[:3])
validator = Validator(property_schema, allow_unknown=True)
for item in result:
validate_or_fail(item, validator)
assert len(result) >= 2
assert len(property_urls) >= 30
if os.getenv("SAVE_TEST_RESULTS") == "true":
result.sort(key=lambda x: x["url"])
(Path(__file__).parent / 'results/properties.json').write_text(
json.dumps(result, indent=2, ensure_ascii=False, default=str)
)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_provinces_scraping():
result = await idealista.scrape_provinces(
urls=["https://www.idealista.com/venta-viviendas/almeria-provincia/municipios"]
)
assert len(result) >= 2
if os.getenv("SAVE_TEST_RESULTS") == "true":
result.sort()
(Path(__file__).parent / 'results/search_URLs.json').write_text(
json.dumps(result, indent=2, ensure_ascii=False, default=str)
)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_search_scraping():
result = await idealista.scrape_search(
url="https://www.idealista.com/en/venta-viviendas/marbella-malaga/con-chalets/",
# remove the max_scrape_pages paremeter to scrape all pages
max_scrape_pages=3,
)
validator = Validator(search_schema, allow_unknown=True)
for item in result:
validate_or_fail(item, validator)
for k in search_schema:
require_min_presence(result, k, min_perc=search_schema[k].get("min_presence", 0.1))
assert len(result) > 60
if os.getenv("SAVE_TEST_RESULTS") == "true":
result.sort(key=lambda x: x["link"])
(Path(__file__).parent / 'results/search_data.json').write_text(
json.dumps(result, indent=2, ensure_ascii=False, default=str)
)