forked from dougzor/py_beer_advocate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beer_advocate.py
68 lines (44 loc) · 1.79 KB
/
beer_advocate.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
import requests
from bs4 import BeautifulSoup
beer_advocate_search_url = "http://beeradvocate.com/search"
beer_advocate_beer_page_url = "http://beeradvocate.com/beer/profile/%s/%s"
beer_advocate_brewery_page_url = "http://beeradvocate.com/beer/profile/%s"
def get_beer_list(beer_search):
"""Given a beer_search search term, get all beers from BA that potentially match that search term
"""
payload = {'qt': 'beer', 'q': beer_search}
r = requests.get(beer_advocate_search_url, params=payload)
soup = BeautifulSoup(r.text)
content_element = soup.find(id="content")
try:
results = content_element.find("ul").find_all("li")
beer_urls = []
for result in results:
link = result.find("a")['href']
parts = link.split("/")
beer_id = None
brewery_id = None
if len(parts) == 5:
beer_id = parts[4]
brewery_id = parts[3]
name = result.find("b").text
beer_urls.append([name, beer_id, brewery_id])
return beer_urls
except AttributeError:
return []
def get_brewery_info(brewery_id):
brewery_html = requests.get(beer_advocate_brewery_page_url % brewery_id)
#@todo: do this
return {}
def get_beer_info(brewery_id, beer_id):
"""Given a beer url from get_beer_list() return the information for that beer
"""
beer_html = requests.get(beer_advocate_beer_page_url % (brewery_id, beer_id))
soup = BeautifulSoup(beer_html.text)
brewery = soup.find("div", {"class": "titleBar"}).find("span").string[3:]
score = soup.find("span", {"class": "BAscore_big"}).string
picture = soup.find("img")['src']
data = {"brewery": brewery,
"score": score,
"image": picture}
return data