Skip to content

Instantly share code, notes, and snippets.

@impshum
Forked from skpyns/Cikloberza.py
Created August 14, 2018 11:16
Show Gist options
  • Save impshum/08ccefa8c9e7d396a2212a27bc9c07b0 to your computer and use it in GitHub Desktop.
Save impshum/08ccefa8c9e7d396a2212a27bc9c07b0 to your computer and use it in GitHub Desktop.
Fetching a number of ads of used bicycles
from bs4 import BeautifulSoup
import requests
# reusable main soup function
def lovely_soup(url): # passes the url as a variable
r = requests.get(url) # get the requested url
soup = BeautifulSoup(r.text, 'lxml') # turn html content into soup
return soup
# soup helper function
def items(url): # passes the url as a variable
div = lovely_soup(url).find('div', {'class': 'total'}) # find the single element by class
count = div.findAll('strong')[2].text # get the third strong element text
return count # return the value
# flip the switch
count = items('https://www.2bike.rs/cikloberza/mali-oglasi/delovi-20/ramovi-i-delovi-za-ram-181/ramovi-21?fl20[]=138&fl12[]=72') # pass url to function
print('{} items for sale'.format(count)) # print some stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment