-
-
Save impshum/08ccefa8c9e7d396a2212a27bc9c07b0 to your computer and use it in GitHub Desktop.
Fetching a number of ads of used bicycles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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