-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMLS_scraper_5.py
83 lines (75 loc) · 3.2 KB
/
MLS_scraper_5.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from urllib.request import urlopen
from bs4 import BeautifulSoup
import time
# Here I disabled the function that gets all URLs - for testing purposes
# Added .get_text() for all 6 details I want to capture
# Ran it and found an error: If a tag is missing, e.g. player has no position
# or player has no Twitter - script throws an AttributeError and stops.
# So I wanted to add a try/except but I didn't want to add it 6 times.
# I wrote my 6 details into a list. Then looped over the list with
# a new try/except. If there is an AttributeError, I write "None" --
# this is all inside get_player_details() function
# This works (see MLS_scraper_5.png), but I need to get rid of Age: and
# Birthplace: text.
html = urlopen("http://www.mlssoccer.com/players")
bsObj = BeautifulSoup(html, "html.parser")
p = open("mls_players.txt", 'a')
player_list = []
# player links are on multiple pages -- get the next page URL
def get_next_page(html, bsObj):
next_page = bsObj.find( "a", {"title":"Go to next page"} )
if next_page and ('href' in next_page.attrs):
partial = str(next_page.attrs['href'])
new_url = "http://www.mlssoccer.com" + partial
html = urlopen(new_url)
bsObj = BeautifulSoup(html, "html.parser")
get_player_pages(html, bsObj)
else:
print("Done collecting URLs ...")
# run this on each page to get player detail page links
def get_player_pages(html, bsObj):
global player_list
tag_list = bsObj.findAll( "a", {"class":"row_link"} )
for tag in tag_list:
if 'href' in tag.attrs:
player_list.append(str(tag.attrs['href']))
# else:
# f.write("no href\n")
# delay program for 1 second
time.sleep(1)
# disable for testing - use only first page URLs
# get_next_page(html, bsObj)
def get_player_details(player_list):
for player in player_list:
new_url = "http://www.mlssoccer.com" + player
html = urlopen(new_url)
bsObj = BeautifulSoup(html, "html.parser")
player_details = []
title = bsObj.find( "div", {"class":"title"} )
# div class title (text) player's full name
team = bsObj.find( "div", {"class":"club"} )
# div class club (text) team
position = bsObj.find( "div", {"class":"position"} )
# div class position (text) position
birthday = bsObj.find( "div", {"class":"age"} )
# <div class="age"><span class="category">Age:</span>
# 23 (10/21/1992)</div>
birthplace = bsObj.find( "div", {"class":"hometown"} )
# <div class="hometown"><span class="category">Birthplace:</span>
# Barranquilla, Colombia</div>
twitter = bsObj.find( "div", {"class":"twitter_handle"} )
# <div class="twitter_handle"><a
# href="https://twitter.com/Olmesgarcia13"
# class="twitter_link">@Olmesgarcia13</a></div>
player_details = [title, team, position, birthday, birthplace,
twitter]
for detail in player_details:
try:
print( detail.get_text() )
except AttributeError:
print( "None" )
# delay program for 2 seconds
time.sleep(2)
get_player_pages(html, bsObj)
get_player_details(player_list)
p.close()