/
scrape.py
50 lines (31 loc) · 1.13 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
44
45
46
47
48
49
50
import requests
from BeautifulSoup import BeautifulSoup
import csv
# creates a global variable of the page you are scraping
GLMADIR = "https://glmaimpak.networkats.com/members_online_new/members/dir_provider.asp?action=search&address_zip_radius=25&address_state_code=CA&address_city=san+Francisco&location_type=S&pn=4"
def parse():
"""Gets URL html and selects appropriate areas"""
response = requests.get(GLMADIR)
soup = BeautifulSoup(response.text)
# details of the tag we are searching for
found = soup.findAll("td", {"class": "top", "colspan": "2"}, )
return found
def order(found):
"""Sorts selected info and writes it to file"""
#opens test file to write into
csvf = open("test.csv", "wb")
writer = csv.writer(csvf)
items = [3,5,7,9,11,13,15,17]
#navigates each object and prints key lines
for obj in found:
i = 0
entry = []
for child in obj:
if i == 1:
entry.append(child.text)
if i in items:
entry.append(child)
i += 1
writer.writerow(entry)
parsed = parse()
order(parsed)