Last active
July 9, 2019 21:06
-
-
Save impshum/43e8f21df5bcb9f30758c11a6a70a11e to your computer and use it in GitHub Desktop.
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 | |
from requests import get | |
import csv | |
def main(query, max_pages, outfile): | |
query = query.replace(' ', '%20') | |
page = 1 | |
next = True | |
with open(outfile, 'w+') as csv_file: | |
csv_writer = csv.writer(csv_file) | |
csv_writer.writerow(['Title', 'Price', 'Image Link']) | |
while next: | |
url = f'https://sidelineswap.com/search?q={query}&page={page}' | |
soup = BeautifulSoup(get(url).text, 'lxml') | |
search = soup.find('div', {'class': 'search-page-grid'}) | |
items = search.findAll('div', {'class': 'sls-thumb'}) | |
for item in items: | |
name = item.find('a', {'class': 'sls-thumb--name'}).text | |
price = item.find('div', {'class': 'sls-thumb--price'}).text | |
image = item.find('img', {'class': 'sls-thumb--image'})['src'] | |
csv_writer.writerow([name, price, image]) | |
if soup.find('a', {'rel': 'next'}): | |
page += 1 | |
elif page > max_pages: | |
next = False | |
else: | |
next = False | |
if __name__ == '__main__': | |
main('Maverik Tactik 2.0 Head', 2, 'out.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment