0

I'm trying to scrape links off of Amazon. I ran into a tiny problem.

I wish to add a particular string 'a' to every string in a list.

     links = soup.find_all("a", attrs= {"class":"a-link-normal a-text-normal"})
      urls = [item.get('href') for item in links]
      print(urls)

The output is :

       ['/Redragon-KUMARA-Backlit-Mechanical-Keyboard/dp/B016MAK38U?dchild=1', '/MOTOSPEED-Professional-Mechanical-Keyboard-Illuminated/dp/B07P9J5CBJ?dchild=1', '/Mechanical-Keyboard-Keyboards-Detachable-Programmable/dp/B07QS6MG8B?dchild=1', '/Element-Mechanical-81-Replaceable-Rainbow/dp/B07DYPJWD4?dchild=1', '/DURGOD-Mechanical-Keyboard-Connector-Typists/dp/B07B8DS585?dchild=1']

Now how do I add a = https://amazon.com.au in front of every string in the list output?

I want the output to be as follows:

        ['https://amazon.com.au/Redragon-KUMARA-Backlit-Mechanical-Keyboard/dp/B016MAK38U?dchild=1', 'https://amazon.com.au/MOTOSPEED-Professional-Mechanical-Keyboard-Illuminated/dp/B07P9J5CBJ?dchild=1', 'https://amazon.com.au/Mechanical-Keyboard-Keyboards-Detachable-Programmable/dp/B07QS6MG8B?dchild=1', 'https://amazon.com.au/Element-Mechanical-81-Replaceable-Rainbow/dp/B07DYPJWD4?dchild=1', 'https://amazon.com.au/DURGOD-Mechanical-Keyboard-Connector-Typists/dp/B07B8DS585?dchild=1']

I'm a newbie (1.5 months into python), so apologise in advance if I'm not clear enough.

Thank you!

1 Answers1

1

Possible duplicate of : Appending the same string to a list of strings in Python .

In short -

 urls = ['https://amazon.com.au'+link for link in links]
Idan
  • 26
  • 4
  • I think that your syntax is incorrect. Your adding the string to the beginning of the entire object , making something as such -"https://amazon.com.au['/Redragon-KUMARA-Backlit-Mechanical-Keyboard/dp/B016MAK38U?dchild=1', '/MOTOSPEED-Professional-Mechanical-Keyboard-Illuminated/dp/B07P9J5CBJ?dchild=1'...]" – Idan Jul 31 '21 at 07:27