pythonurlparse

How to convert a string in list into url?


How to convert a string in list into url ? I try url.parse, but it didn't work.

!pip install selenium
from urllib.parse import urlparse
from urllib.parse   import quote
from urllib.request import urlopen
import time

browser = webdriver.Chrome(executable_path='./chromedriver.exe')
wait = WebDriverWait(browser,5)
output = []
for i in range(1,2): # Iterate from page 1 to the last page
    browser.get("https://tw.mall.yahoo.com/search/product?p=%E5%B1%88%E8%87%A3%E6%B0%8F&pg={}".format(i))
    
 wait.until(EC.presence_of_element_located((By.XPATH,"//ul[@class='gridList']")))


    product_links = browser.find_elements(By.XPATH,"//ul[@class='gridList']/li/a")
    
     
    for link in (product_links):
        print(f"{link.get_attribute('href')}")
        output.append([link.get_attribute('href')])


for b in output[:3]:
    print(b)

The total code above, I try to make the string into url. But it doesn't work.


Solution

  • I think what you're trying to do is :

    // importing library 
    from urllib.parse import urlparse
    
    // putting the link in a list 
    b = [['https://tw.mall.yahoo.com/item/p033088522688']
    ['https://tw.mall.yahoo.com/item/p0330103147501']
     ['https://tw.mall.yahoo.com/item/p033097510324']]
    
    // going through each element of the list and parse them 
    for i in range ( len (b)) : 
         print(urlparse(b[i]))