Skip to content

Instantly share code, notes, and snippets.

@k0mmsussert0d
Created September 4, 2019 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0mmsussert0d/8fa7b7d75b26366ebeafbb79904f9262 to your computer and use it in GitHub Desktop.
Save k0mmsussert0d/8fa7b7d75b26366ebeafbb79904f9262 to your computer and use it in GitHub Desktop.
Scraping suru product IDs
"""
This script will print the IDs of all the products returned by a given search query.
Substitue url (line 17) string with any jewgaya query. The script will stop after reaching the last page.
The URL must be properly encoded according to RFC 3986.
"""
from bs4 import BeautifulSoup
import requests
def get_soup(url):
content = requests.get(url)
return BeautifulSoup(content.text, 'html.parser')
address = 'https://www.suruga-ya.jp'
url = ''
soup = get_soup(url)
while True:
titles = soup.find_all('p', class_='title')
for item in titles:
a = item.find('a').attrs['href']
link = a.rsplit('/')[-1]
print(link)
next_page = soup.find('li', class_='next')
if next_page is not None:
url_next = address + next_page.find('a').attrs['href']
soup = get_soup(url_next)
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment