Created
September 4, 2019 21:52
-
-
Save k0mmsussert0d/8fa7b7d75b26366ebeafbb79904f9262 to your computer and use it in GitHub Desktop.
Scraping suru product IDs
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
""" | |
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