Snippets
Created by
Dénes Türei
last modified
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import imp
import sys
import bs4
import socket
import urllib.request
import http.client
import gzip
from io import BytesIO
import json
import itertools
import time
try:
import brotli
except ImportError:
sys.stdout.write(
'Module `brotli` not available.\n'
'Won\'t be able to extract Brotli compressed responses.\n'
'You can install brotli by `pip install brotli`.\n'
)
# author Dénes Türei
# turei.denes@gmail.com
# 2018-09-24:
# -- added Brotli compression handler
# 2018-07-29:
# -- catching also URLError and ConnectionResetError
# 2018-07-25:
# -- error handling for failed downloads: 3 attempts & giving up
# -- list of all towns added to sections
# 2018-03-02
# -- created
class Ingatlan(object):
baseurl = 'https://ingatlan.com/'
phoneurl = 'https://ingatlan.com/detailspage/api/%u'
sections = [
'budapest',
'nyiregyhaza',
'debrecen',
'miskolc',
'szeged',
'gyor',
'pecs',
'szombathely',
'sopron',
'vac',
'budaors',
'szentendre',
'szekesfehervar',
'veszprem',
'szolnok',
'szekszard',
'tatabanya',
'zalaegerszeg',
'salgotarjan',
'kaposvar',
'kecskemet'
]
subsections = ['kiado+lakas']
http_headers = {
'Host': 'ingatlan.com',
'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:54.0) Gecko/20110304 Firefox/54.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://ingatlan.com/',
'Cookie': 'ICSESSIDfrontend=nk313j4alqbjsvaj8dp84fm7t4; m1x_switch=off; G_ENABLED_IDPS=google; nps_hidden=1; bbid=9036898247414400',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'
}
# if debug messages needed:
#opener = urllib.request.build_opener(
#urllib.request.HTTPHandler(debuglevel=1),
#urllib.request.HTTPSHandler(debuglevel=1)
#)
#urllib.request.install_opener(opener)
def __init__(self):
self.ad_urls = set([])
self.data = {}
def reload(self, children = False):
modname = self.__class__.__module__
mod = __import__(modname, fromlist=[modname.split('.')[0]])
imp.reload(mod)
new = getattr(mod, self.__class__.__name__)
setattr(self, '__class__', new)
def main(self):
self.iter_sections()
self.export_urls()
self.iter_ads()
self.export_ads()
def iter_sections(self):
# sections are defined manually above
for sec, subsec in itertools.product(self.sections, self.subsections):
# starting from page 1
page = 0
# iterating pages:
while True:
# next page
page += 1
# constructing the URL
self.url = '%s%s/%s?page=%u' % (
self.baseurl, sec, subsec, page
)
# generic method for download something
# and extract HTML
self.load_page()
# if the download failed:
if not self.html:
continue
# create a BeautifulSoup object
self.soup = bs4.BeautifulSoup(self.html, 'lxml')
# parse HTML of advertisement list
self.parse_page()
# stop if we have no more results:
if self.resn == '0':
sys.stdout.write(
'[ INFO ] No more URLs, '
'section `%s/%s` finished\n' % (
sec, subsec
)
)
break
def load_page(self, method = 'GET', data = None):
response = None
# creating request
req = urllib.request.Request(self.url, method = method, data = data)
# setting headers
for key, val in self.http_headers.items():
req.add_header(key, val)
sys.stdout.write('[ INFO ] Attempting to load `%s`\n' % self.url)
for i in range(3):
try:
# opening connection
try:
response = urllib.request.urlopen(req, timeout = 90)
except UnicodeEncodeError:
sys.stdout.write(
'[ WARN ] Could not ascii encode: `%s`\n' % self.url
)
self.html = None
break
break
except (
urllib.error.HTTPError,
ConnectionResetError,
urllib.error.URLError,
socket.timeout
) as e:
sys.stdout.write(
'%s`--> Failed with `%s`, trying again. Attempt #%u\n' % (
' ' * 13,
e.__class__.__name__,
i + 2
)
)
time.sleep(2)
if not response:
sys.stdout.write(
'[ WARN ] Failed to download `%s`. Giving up.\n' % self.url
)
self.html = None
return
resp_encoding = response.info()['Content-Encoding']
# loading response into BytesIO
response_file = BytesIO(response.read())
# read 2 bytes
magic = response_file.read(2)
response_file.seek(0)
# extracting HTML from response
if magic == b'\x1f\x8b' or resp_encoding == 'gzip':
# gzip extraction
self.html = gzip.GzipFile(
fileobj = response_file
).read().decode('utf-8') # decoding bytes to UTF-8
elif resp_encoding == 'br':
# brotli extraction
self.html = brotli.decompress(response_file.read()).decode('utf-8')
else:
# assume no compression
self.html = response_file.read().decode('utf-8')
#sys.stdout.write('[ !! ] Error downloading `%s`\n' % self.url)
def parse_page(self):
# check if we have more than zero result
self.resn = self.soup.find(
'span',
{'class': 'results__number__count'}
).text.strip()
# these are the individual ads on one page
for card in self.soup.find_all('div', {'class': 'listing__card'}):
# the very first anchor is the ad URL
# we collect these in the ad_urls set:
self.ad_urls.add(card.find('a').get('href'))
def iter_ads(self):
# iterate advertisement URLs
for ad_url in self.ad_urls:
self.url = '%s%s' % (self.baseurl, ad_url)
self.load_page()
# if the download failed:
if not self.html:
continue
self.soup = bs4.BeautifulSoup(self.html, 'lxml')
self.parse_ad()
def export_urls(self, fname = 'urls.txt'):
# writing URLs to file
with open(fname, 'w') as fp:
_ = fp.write('\n'.join(
'%s%s' % (self.baseurl, url)
for url in self.ad_urls
))
def get_phone(self, adid):
self.url = self.phoneurl % int(adid)
data = ((
'{"id":%u,"is_favourite":false,"is_hidden":false,'
'"is_phone_number_visible":true,"phone_numbers":[]}'
) % int(adid)).encode('ascii')
self.http_headers['Content-type'] = 'application/json'
self.load_page(method = 'PUT', data = data)
del self.http_headers['Content-type']
return (
''
if not self.html else
';'.join(json.loads(self.html)['phone_numbers'])
)
def parse_ad(self):
def get_param(div, sub = 'parameter-value'):
try:
param = self.soup.find('div', {'class': div}).find(
'span', {'class': sub}).text
except:
param = 'none'
return param
# here write some BeautifulSoup code to extract info
# from an individual ad
ad_url = self.url
try:
title = self.soup.find('h1', {'class': 'js-listing-title'}).text
except:
title = 'none'
try:
subtype = self.soup.find('div', {'class': 'listing-subtype'}).text
except:
title = 'none'
price = get_param('parameter-price')
area = get_param('parameter-area-size')
rooms = get_param('parameter-room')
try:
desc = self.soup.find('div', {'class': 'long-description'}).text
except:
desc = 'none'
try:
agent = self.soup.find('div', {'class': 'agent-name'}).text
except:
agent = 'none'
adid = self.soup.find('b', {'class': 'listing-id'}).text
phone = self.get_phone(adid)
# here a typo in webpage code :)
dparam = self.soup.find('div', {'class': 'paramterers'})
param = {}
for tab in dparam.find_all('table'):
for tr in tab.find_all('tr'):
td0 = tr.find('td')
key = td0.text
val = td0.find_next_sibling().text
param[key] = val
self.data[ad_url] = {
'title': title,
'id': adid,
'subtype': subtype,
'price': price,
'area': area,
'rooms': rooms,
'agent': agent,
'phone': phone,
'desc': desc,
'param': param
}
def export_ads(self, fname = 'ads.tsv'):
with open(fname, 'w') as fp:
hdr = [
'url',
'id',
'title',
'subtype',
'price',
'area',
'rooms',
'agent',
'phone',
'desc',
'param'
]
fp.write('%s\n' % ('\t'.join(hdr)))
for url, data in self.data.items():
fp.write('%s\t%s\t%s\n' % (
url,
'\t'.join(
# except url and param
data[h] for h in hdr[1:-1]
),
';'.join('%s|%s' % i for i in data['param'].items())
))
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.