naufalnashif commited on
Commit
457213e
·
1 Parent(s): 932884f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -221,14 +221,18 @@ def scrape_tokped_with_selenium(nama_barang, num_items):
221
  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='
222
 
223
  driver.get(url)
224
- time.sleep(10)
225
-
 
 
226
  html = driver.page_source
 
 
227
  soup = BeautifulSoup(html, "html.parser")
228
- # Dapatkan HTML dari elemen
229
- #html = html_element.get_attribute("innerHTML")
230
- #html = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
231
- product_container_list = soup.find_all('a', class_="pcv3__info-content css-gwkf0u", href = True)
232
 
233
  for product_info in product_container_list:
234
  link = product_info['href']
@@ -263,7 +267,7 @@ def scrape_tokped_with_selenium(nama_barang, num_items):
263
  if len(products) >= num_items:
264
  products = products[:num_items]
265
  break
266
- page += 1
267
  except requests.exceptions.RequestException as e:
268
  logging.error(f"Terjadi kesalahan saat mengirim permintaan: {e}")
269
  st.error(f"Terjadi kesalahan saat mengirim permintaan: {e}")
@@ -282,7 +286,7 @@ def scrape_tokped_with_selenium(nama_barang, num_items):
282
  finally:
283
  if driver:
284
  driver.quit()
285
-
286
  return products
287
  #---------------------------------------------------User Interface----------------------------------------------------------------------
288
 
 
221
  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='
222
 
223
  driver.get(url)
224
+ # Tunggu hingga elemen tertentu muncul (gunakan sesuai kebutuhan)
225
+ WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "pcv3__info-content css-gwkf0u")))
226
+
227
+ # Dapatkan sumber halaman setelah eksekusi JavaScript
228
  html = driver.page_source
229
+
230
+ # Gunakan BeautifulSoup untuk melakukan parsing HTML
231
  soup = BeautifulSoup(html, "html.parser")
232
+
233
+ # Cari semua elemen yang sesuai
234
+ product_container_list = soup.find_all('a', class_="pcv3__info-content css-gwkf0u", href=True)
235
+
236
 
237
  for product_info in product_container_list:
238
  link = product_info['href']
 
267
  if len(products) >= num_items:
268
  products = products[:num_items]
269
  break
270
+
271
  except requests.exceptions.RequestException as e:
272
  logging.error(f"Terjadi kesalahan saat mengirim permintaan: {e}")
273
  st.error(f"Terjadi kesalahan saat mengirim permintaan: {e}")
 
286
  finally:
287
  if driver:
288
  driver.quit()
289
+ page += 1
290
  return products
291
  #---------------------------------------------------User Interface----------------------------------------------------------------------
292