Skip to content

Instantly share code, notes, and snippets.

@tylerburdsall
Created September 6, 2017 23:39
Show Gist options
  • Save tylerburdsall/9dd81bab5df4dae8bca6f1b417e52df2 to your computer and use it in GitHub Desktop.
Save tylerburdsall/9dd81bab5df4dae8bca6f1b417e52df2 to your computer and use it in GitHub Desktop.
from urllib.request import urlopen
from bs4 import BeautifulSoup
# add new modules
import textwrap
import texttable as tt
website = 'https://saltandstraw.com/flavors/'
page = urlopen(website)
soup = BeautifulSoup(page, 'html.parser')
results = soup.find('section', attrs={'class':'content-area clear portland'}).findAll('div', attrs={'class':'entry-title'})
flavors = []
for title in results:
flavors.append(title.text)
count = len(flavors)
flavors_and_links = []
links = soup.find('section', attrs={'class':'content-area clear portland'}).findAll('a', href=True)
for i in range(count):
flavors_and_links.append({'Flavor': flavors[i], 'Link': links[i]['href']}) # new change
descs = []
for i in range(count):
link = flavors_and_links[i]['Link']
page = urlopen(link)
soup = BeautifulSoup(page, 'html.parser')
result = soup.find('span', attrs={'style':'font-weight: 400;'})
description = textwrap.wrap(result.text, width=40) # wrap the text
desc_val = ""
for j in description: # append each line to the description
desc_val += j + '\n'
descs.append(desc_val) # add the description
# new code below
tab = tt.Texttable()
headings = ['Flavor', 'Description'] # set the headers for the table
tab.header(headings)
# Create and print the table
for row in zip(flavors, descs):
tab.add_row(row)
s = tab.draw()
print(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment