Skip to content

Instantly share code, notes, and snippets.

@cafca
Last active October 9, 2015 20:49
Show Gist options
  • Save cafca/1d130102aaeef3df6fde to your computer and use it in GitHub Desktop.
Save cafca/1d130102aaeef3df6fde to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Immoscout Crawler\n",
"\n",
"Loads Immoscout results from url, parse and display as text"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The soup extension is already loaded. To reload it, use:\n",
" %reload_ext soup\n"
]
}
],
"source": [
"import re\n",
"import requests\n",
"import time\n",
"from datetime import datetime\n",
"from IPython.display import display_pretty, clear_output\n",
"%load_ext soup"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"configure_ipython_beautifulsoup(show_html=False, show_css=False)"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"urls = list()\n",
"urls.append(\"http://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Umkreissuche/Berlin/10967/228521/2509075/M_fcllenhoffstra_dfe/-/3/3,00-/75,00-/EURO--1100,00/-/-/false/-/-/-/-/-/-/-/-1940/-/-/-/-/1-\")\n",
"urls.append(\"http://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Umkreissuche/Berlin/10967/228521/2509075/M_fcllenhoffstra_dfe/-/3/4,00-/95,00-/EURO--1400,00/-/-/false/-/-/-/-/-/-/-/-1940/-/-/-/-/1-\")\n",
"urls.append(\"http://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Umkreissuche/Berlin/10551/223304/2512943/Bredowstra_dfe/-/1/3,00-/80,00-/EURO--1100,00/-/-/false/-/-/-/-/-/-/-/-1940/-/-/-/-/1-?enteredFrom=result_list\")\n",
"urls.append(\"http://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Umkreissuche/Berlin/10829/225175/2508329/Hohenfriedbergstra_dfe/-/1/3,00-/80,00-/EURO--1100,00/-/-/false/-/-/-/-/-/-/-/-1940/-/-/-/-/1-?enteredFrom=result_list\")"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"known_ids = set([\"84672385\",\"84607255\",\"65881497\",\"81156001\",\"74122894\",\"84441189\",\"84296913\",\"83082603\",\n",
" \"83968655\",\"75090852\",\"84487728\",\"84459163\",\"84441205\",\"73972731\",\"83966216\",\"83256999\",\n",
" \"84666872\",\"54065226\",\"84468125\",\"83875423\",\"83819962\",\"83947419\",\"83544107\",\"82957811\",\n",
" \"81024598\",\"55481227\",\"64694875\",\"75825617\"])"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Find the ad's link in its entry\n",
"get_link = lambda res: \"http://www.immobilienscout24.de{}\".format(res.find('a', {\"class\": \"logo_link\"}).get('href'))"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Return a list of ad texts from parsing a given url\n",
"def result_list(url):\n",
" soup = p(url)\n",
" results = soup.find_all('div', {\"class\": 'result_entry_box'})\n",
" return [\"{}\\n{}\".format(\" \".join(res.stripped_strings).encode('utf-8'), get_link(res)) for i, res in enumerate(results)]"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22:42:57.712692\n"
]
}
],
"source": [
"run = 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"try:\n",
" while True:\n",
" run += 1\n",
" print \"Run #{} {}\".format(run, datetime.now().time())\n",
" for url in urls:\n",
" for res in result_list(url):\n",
" id_matches = re.findall(\"\\d{8}\", res)\n",
" if id_matches[0] not in known_ids:\n",
" !say \"Yeah! Found a new place, mhkae?\"\n",
" print res\n",
" known_ids.add(res)\n",
" time.sleep(1)\n",
" time.sleep(180)\n",
"except Exception:\n",
" !say \"Oh crap! It's broken\"\n",
" print \"\\n\\nKnown ids: {}\\n\\n\".format(known_ids)\n",
" raise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment