PIEGE
Newbie
- Jul 4, 2019
- 35
- 7
Seeing a few threads about python. I like the topic so I hope to make a pitch a small contribution.
I still have newbie status so i cant post links, in some cases you will have to use google fu.
also python has alot of tools that go way deep, i can't explain in depth what every single component does but everything in play is well supported.
so lets break some shit...
I still have newbie status so i cant post links, in some cases you will have to use google fu.
also python has alot of tools that go way deep, i can't explain in depth what every single component does but everything in play is well supported.
so lets break some shit...
Code:
# i like ugly shoes, so as a fan, i use some python to track the sneaker game.
# some imports we got:
# json: some tools for making json orderly ( you can build json with strings but i like the convenience of tools... )
# beautiful soup: so requests_html has alot of tools for parsing json,html, et. al. but i'm doing a little bs4 as a crowd pleaser.
# requests_html: some additions to the python 'requests' this topic goes way too deep for this example so you gotta do homework
# sermon.quit(00piege)
import json
from bs4 import BeautifulSoup
from requests_html import HTMLSession
# set up a session
session = HTMLSession()
# i like to name a base url in simple testing
baseUrl = 'yeezysupply' # this would be yeezysupply dot com but you know.. the link thing.
#products = '/products'
#collections = '/collections'
# some ancillary properties
# didn't use them here but if i did, would have given it some variable to maybe add some flexibility later.
# variant_by_name = boost350black
# url to session object
# python has very nice string formatting, give it a try
r = session.get("{}".format(baseUrl))
#page as soup object
soup = BeautifulSoup(r.content, "html.parser")
# now lets start working back through the main page
pageBody = soup.body
# one of the javascript scripts of yeezysupply gives back a bunch of json
# so we can sidestep fumbling through parsing html to get producs
# and instead grab the json the script produces...
# i work with this data in a variable called 'feature'
feature = pageBody.find('script').text
jsonFeature = json.loads(feature)
# count of products
products_count = jsonFeature['products_count']
# products..
jsonProducts = jsonFeature['products']
print(jsonProducts)
# cheers