-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscrape.py
43 lines (38 loc) · 1.82 KB
/
scrape.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
# -*- coding: utf-8 -*-
from operator import methodcaller
from itertools import product
import scrapy
import codecs
class PostalScraper(scrapy.Spider):
name = 'PostalSpider'
start_urls = ['http://www.posten.se/soktjanst/postnummersok/gb/index_v.jspv']
def parse(self, response):
zip_codes = product(range(1, 10), repeat=5)
for code in zip_codes:
code = ''.join([str(digit) for digit in code])
yield scrapy.http.FormRequest.from_response(
response,
formdata={'pnr': code},
callback=lambda r, code=code: self.after_post(r, code)
)
def after_post(self, response, code):
if "Your search produced no hits." not in response.body:
with codecs.open('postcodes.csv', "ab", "utf-8") as csv_file:
with codecs.open('errors.csv', "ab", "utf-8") as errors_file:
for result in response.css('table.result').css('tr'):
try:
if len(result.css('td::text').extract()) == 3:
street_name, post_code, city = \
result.css('td::text').extract()
po_box_no = ''
else:
street_name, po_box_no, post_code, city = \
result.css('td::text').extract()
titler = methodcaller('title')
txt = u','.join(
map(titler,
(street_name, po_box_no, post_code, city))
)
csv_file.write(u'%s,%s\n' % (code, txt))
except:
errors_file.write('%s\n' % code)