-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_builds_urls.py
93 lines (78 loc) · 2.59 KB
/
get_builds_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
91
92
93
import time
import logging
import argparse
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)
id_page = 0
builds = []
old_links = []
while True:
id_page = id_page + 1
url = f"https://pcpartpicker.com/builds/#page={id_page}"
logger.debug("browser.get(%s)", url)
browser.get(url)
number_of_tries = 0
while True:
number_of_tries += 1
time.sleep(4)
if number_of_tries > 10:
logger.warning("More than 10 tries. Waiting 30 seconds...")
browser.get(url)
time.sleep(30)
soup = BeautifulSoup(browser.page_source, "lxml")
buildlinks = soup.find_all("a", {"class": "logGroup__target"})
if not buildlinks:
logger.debug("No links found. Exiting")
break
if buildlinks != old_links:
logger.debug(
"Different links than before. Going to the next page"
)
old_links = buildlinks
break
logger.warning(
"Same links extracted twice. Retrying in 4 seconds.."
)
old_links = buildlinks
soup.decompose()
logger.debug("buildlinks : %s", buildlinks)
if not buildlinks:
break
for buildlink in buildlinks:
builds.append(str(buildlink["href"]))
# break
logger.debug("builds : %s", builds)
directory = "Exports"
Path(directory).mkdir(parents=True, exist_ok=True)
with open("Exports/list_builds_urls.txt", "w") as f:
for build in builds:
f.write(f"https://pcpartpicker.com{build}\n")
print("Runtime : %.2f seconds" % (time.time() - temps_debut))
def parse_args():
parser = argparse.ArgumentParser(
description="Scraper pcpartpicker.com (builds_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()