-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_parts_urls.py
90 lines (75 loc) · 2.47 KB
/
get_parts_urls.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import time
import logging
import argparse
import requests
import itertools
from bs4 import BeautifulSoup
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
logger = logging.getLogger()
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("selenium").setLevel(logging.WARNING)
temps_debut = time.time()
def main():
args = parse_args()
options = Options()
options.headless = True
browser = webdriver.Firefox(options=options)
url_index = "https://pcpartpicker.com/products"
soup_index = BeautifulSoup(
requests.get(url_index).content, features="lxml"
)
cat_products = [
x
for y in soup_index.findAll("div", {"class": "block"})
for x in y.findAll("li")
if y.find("li")
]
soup_index.decompose()
all_products = []
for cat in cat_products:
index_page = 0
while True:
index_page += 1
cat_link = f"https://pcpartpicker.com{cat.find('a')['href']}#page={index_page}"
# cat_name = cat.text
logger.debug("Catégorie : %s, Page : {index_page}", cat)
browser.get(cat_link)
time.sleep(5)
soup = BeautifulSoup(browser.page_source, "lxml")
products = soup.find_all("td", {"class": "td__name"})
soup.decompose()
if not products:
logger.debug("No products found")
break
products = [
f"https://pcpartpicker.com{x.find('a')['href']}"
for x in products
]
all_products.append(products)
all_products = list(itertools.chain.from_iterable(all_products))
logger.debug("products : %s", all_products)
directory = "Exports"
Path(directory).mkdir(parents=True, exist_ok=True)
with open("Exports/list_parts_urls.txt", "w") as f:
for part in all_products:
f.write(f"{part}\n")
print("Runtime : %.2f seconds" % (time.time() - temps_debut))
def parse_args():
parser = argparse.ArgumentParser(
description="Scraper pcpartpicker.com (parts-urls)"
)
parser.add_argument(
"--debug",
help="Display debugging information",
action="store_const",
dest="loglevel",
const=logging.DEBUG,
default=logging.INFO,
)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
return args
if __name__ == "__main__":
main()