I've tried to decode the URL if present, but it doesn't seem to work:
Here is the error shown below:
Traceback (most recent call last):
File "E:\Users\Francbicon\Desktop\Bots\Master Copy\Shopee Endless Loop.py", line 185, in <module>
clickpy()
File "E:\Users\Francabicon\Desktop\Bots\Master Copy\Shopee Endless Loop.py", line 75, in clickpy
print(all_urls)
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 878-878: Non-BMP character not supported in Tk
Here is the whole code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import csv
import urllib.parse
import time
url = 'https://shopee.com.my/search?keyword=mattress'
driver = webdriver.Chrome(executable_path=r'E:/users/Asashin/Desktop/Bots/others/chromedriver.exe')
driver.get(url)
time.sleep(0.8)
# Select language
driver.find_element_by_xpath('//div[@class="language-selection__list"]/button').click()
time.sleep(3)
# Scroll a few times to load all items
def clickpy():
for x in range(10):
driver.execute_script("window.scrollBy(0,300)")
time.sleep(0.1)
# Get all links (without clicking)
all_items = driver.find_elements_by_xpath('//a[@data-sqe="link"]')
all_urls = []
s=["-Dr.Alstone-","-Dr.-Alstone-","-Lutfy-Paris-"]
for item in all_items:
# This give you whole url of the anchor tag
url = item.get_attribute('href')
if "-Dr.Alstone-" in url:
continue
else:
if "-Dr.-Alstone-" in url:
continue
else:
if "/Dr.Alstone-" in url:
continue
else:
if "-Simoni-" in url:
continue
else:
if "-Lütfy-" in url:
continue
else:
# You need to remove the preceding values in order to verify href later for clicking
urlfinal=url.split('https://shopee.com.my')[1]
c = urllib.parse.unquote(urlfinal)
all_urls.append(c)
print(all_urls)
a= len(all_urls)
print('len:' + str(a))
Here is the thing I've tried: try and except, if-else and normal looping, but it doesn't seem to work quite well. The error keeps popping it out.
How do I fix it?