Commit
·
b68f935
1
Parent(s):
97fe47a
Update app.py
Browse files
app.py
CHANGED
@@ -75,16 +75,19 @@ def scrape_tokped(nama_barang, num_items):
|
|
75 |
while len(products) < num_items :
|
76 |
|
77 |
url = f'https://www.tokopedia.com/search?navsource=&page={page}&q={query}&srp_component_id=02.01.00.00&srp_page_id=&srp_page_title=&st='
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
headers = {
|
79 |
-
'User-Agent':
|
80 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
81 |
-
'Accept-Encoding': 'gzip, deflate, br',
|
82 |
-
'Connection': 'keep-alive',
|
83 |
-
'Referer': 'https://www.google.com' # Ganti dengan URL referer yang sesuai
|
84 |
}
|
85 |
-
timeout =
|
86 |
try :
|
87 |
-
|
88 |
response = requests.get(url, headers = headers, timeout = timeout)
|
89 |
response.raise_for_status()
|
90 |
|
@@ -93,7 +96,6 @@ def scrape_tokped(nama_barang, num_items):
|
|
93 |
|
94 |
for product_info in product_container_list:
|
95 |
link = product_info['href']
|
96 |
-
st.write(link)
|
97 |
title_element = product_info.find('div', class_="prd_link-product-name css-3um8ox")
|
98 |
title = title_element.text.strip() if title_element else None
|
99 |
|
@@ -125,16 +127,7 @@ def scrape_tokped(nama_barang, num_items):
|
|
125 |
products = products[:num_items]
|
126 |
break
|
127 |
|
128 |
-
except requests.exceptions.RequestException as e:
|
129 |
-
logging.error(f"Terjadi kesalahan saat mengirim permintaan: {e}")
|
130 |
-
st.error(f"Terjadi kesalahan saat mengirim permintaan: {e}")
|
131 |
-
break
|
132 |
-
except requests.exceptions.HTTPError as e:
|
133 |
-
logging.error(f"HTTP Error: {e}")
|
134 |
-
st.error(f"HTTP Error: {e}")
|
135 |
-
break
|
136 |
except Exception as e:
|
137 |
-
logging.error(f"Terjadi kesalahan yang tidak diketahui: {e}")
|
138 |
st.error(f"Terjadi kesalahan yang tidak diketahui: {e}")
|
139 |
break
|
140 |
page += 1
|
|
|
75 |
while len(products) < num_items :
|
76 |
|
77 |
url = f'https://www.tokopedia.com/search?navsource=&page={page}&q={query}&srp_component_id=02.01.00.00&srp_page_id=&srp_page_title=&st='
|
78 |
+
user_agents = [
|
79 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
|
80 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134",
|
81 |
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15",
|
82 |
+
]
|
83 |
+
|
84 |
+
# Mendapatkan User-Agent acak
|
85 |
+
random_user_agent = random.choice(user_agents)
|
86 |
headers = {
|
87 |
+
'User-Agent': random_user_agent
|
|
|
|
|
|
|
|
|
88 |
}
|
89 |
+
timeout = 10
|
90 |
try :
|
|
|
91 |
response = requests.get(url, headers = headers, timeout = timeout)
|
92 |
response.raise_for_status()
|
93 |
|
|
|
96 |
|
97 |
for product_info in product_container_list:
|
98 |
link = product_info['href']
|
|
|
99 |
title_element = product_info.find('div', class_="prd_link-product-name css-3um8ox")
|
100 |
title = title_element.text.strip() if title_element else None
|
101 |
|
|
|
127 |
products = products[:num_items]
|
128 |
break
|
129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
except Exception as e:
|
|
|
131 |
st.error(f"Terjadi kesalahan yang tidak diketahui: {e}")
|
132 |
break
|
133 |
page += 1
|