-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
36 lines (32 loc) · 1.69 KB
/
scraper.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
###############################################################################
# START HERE: Tutorial 3: More advanced scraping. Shows how to follow 'next'
# links from page to page: use functions, so you can call the same code
# repeatedly. SCROLL TO THE BOTTOM TO SEE THE START OF THE SCRAPER.
###############################################################################
import scraperwiki
import lxml.html
# scrape_table function: gets passed an individual page to scrape
# scrape_and_look_for_next_link function: calls the scrape_table
# function, then hunts for a 'next' link: if one is found, calls itself again
def scrape_and_look_for_next_link(url):
html = scraperwiki.scrape(url)
print html
root = lxml.html.fromstring(html)
rows = root.cssselect("tr") # selects all <tr> blocks within <table class="data">
for row in rows:
# Set up our data record - we'll need it later
record = {}
table_cells = row.cssselect("td p.ex1")
if table_cells:
record['RaceCourse'] = table_cells[0].text_content
record['Information'] = table_cells[1].text_content
# Print out the data we've gathered
print record, '------------'
# Finally, save the record to the datastore - 'Artist' is our unique key
scraperwiki.sqlite.save(["Race_Courses_new"], record)
# ---------------------------------------------------------------------------
# START HERE: define your starting URL - then
# call a function to scrape it.
# ---------------------------------------------------------------------------
starting_url = 'http://www.ukjockey.com/racecourses.html'
scrape_and_look_for_next_link(starting_url)