raena-crawler-engine/hasaki_crawler_engine/test_selenium.py

45 lines
1.5 KiB
Python
Raw Normal View History

2024-04-03 08:36:43 +00:00
from seleniumwire import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import logging
from fake_useragent import UserAgent
import brotli
import seleniumwire.undetected_chromedriver as uc
import json
def get_raw_product_data_selenium(url):
ua = UserAgent(platforms='mobile')
random_mobile_ua = ua.random
logging.info("using user agent: {}".format(random_mobile_ua))
op = uc.ChromeOptions()
op.add_argument(f"user-agent={random_mobile_ua}")
op.add_experimental_option("useAutomationExtension", False)
op.add_argument('--no-sandbox')
op.add_argument('--disable-notifications')
op.add_argument("--lang=en-GB")
op.headless = False
driver = uc.Chrome(service=Service(ChromeDriverManager().install()), options=op)
driver.get(url)
iteminfo = ""
for request in driver.requests:
if request.response:
if '/wap/v2/product/detail' in request.url:
encoding = request.response.headers.get('content-encoding')
# logging.info(encoding)
if encoding:
iteminfo = brotli.decompress(request.response.body)
else:
iteminfo = request.response.body
driver.quit()
iteminfo_json = json.loads(iteminfo)
print(iteminfo_json)
get_raw_product_data_selenium('https://hasaki.vn/san-pham/tinh-chat-chong-nang-sunplay-hieu-chinh-sac-da-50g-xanh-87613.html')