This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraping.py
61 lines (54 loc) · 1.72 KB
/
scraping.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
import requests
import time
from bs4 import BeautifulSoup
import time
import json
top_url = 'http://xn--4dkp5a8a4562a1c2fvhm.com/shoplist'
res = requests.get(top_url)
soup = BeautifulSoup(res.content, 'lxml')
article = soup.find(attrs={'class': 'article'})
rows = article.find_all('a')
restaurant_links = [{'url':row.get('href'), 'name': row.text} for row in rows]
data = []
def convert_address(address_text):
address_text = address_text.replace('〒', '').strip()
if address_text[0].isdigit():
zipcode, address = address_text.split(' ', 1)
else:
zipcode = ''
address = address_text
return zipcode, address
for row in restaurant_links:
url = row['url']
res = requests.get(url)
soup = BeautifulSoup(res.content, 'lxml')
tds = soup.find_all('td')
name = tds[1].text.strip()
zipcode, address = convert_address(tds[3].text)
tel = tds[5].text
opens = tds[7].text
holidays = tds[9].text
access = tds[11].text
opened_in = tds[13].text
menu = tds[15].text
# get coordination
soup_geo = BeautifulSoup(requests.get('http://www.geocoding.jp/api/?v=1.1&q='+address).content, 'xml')
print(float(soup_geo.lat.text))
data.append({
'name': name,
'zipcode': zipcode,
'address': address.strip(),
'tel': tel,
'opens': opens,
'holidays': holidays,
'access': access,
'opened_in': opened_in,
'menu': menu,
'coordinate': {'lat': float(soup_geo.lat.text), 'lng': float(soup_geo.lng.text)}
})
print(data)
time.sleep(3)
else:
jsondata = {'restaurants': data}
with open('restaurants.json', 'wb') as f:
f.write(json.dumps(jsondata, ensure_ascii = False).encode("utf8"))