-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
45 lines (30 loc) · 1.09 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import time
import urllib.parse
import requests
from bs4 import BeautifulSoup
# Constants
REF = '77080'
COLOR = 'ブラック'
INDEX = 'アラビア'
GUARANTEE = '保証書'
BASE_URL = 'https://www.kame-kichi.com/mixed-search'
def build_full_url(base_url, relative_url):
return urllib.parse.urljoin(base_url, relative_url)
def fetch_html(url):
response = requests.get(url)
response.raise_for_status()
return BeautifulSoup(response.content, 'html.parser')
def item_matches_specs(item_soup, color, index, guarantee):
specs = item_soup.find(class_='styles_specs__B4WEM').text
return all(keyword in specs for keyword in [color, index, guarantee])
def main():
search_url = f'{BASE_URL}?word={REF}#items'
search_soup = fetch_html(search_url)
for elm in search_soup.find(class_='styles_items__PTKAI').find_all('a'):
link_url = build_full_url(BASE_URL, elm.get('href'))
item_soup = fetch_html(link_url)
if item_matches_specs(item_soup, COLOR, INDEX, GUARANTEE):
print(link_url)
time.sleep(1)
if __name__ == '__main__':
main()