如何添加到列表中的字符串? (从亚马逊抓取链接)
How do I add to strings in a list? (scraping links off Amazon)
我正在尝试从 Amazon 上删除链接。我运行陷入了一个小问题。
我希望将特定字符串 'a' 添加到列表中的每个字符串。
links = soup.find_all("a", attrs= {"class":"a-link-normal a-text-normal"})
urls = [item.get('href') for item in links]
print(urls)
输出为:
['/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']
现在如何在列表输出中的每个字符串前面添加一个 = https://amazon.com.au?
我希望输出如下:
['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']
我是新手(进入 python 1.5 个月),如果我不够清楚,请提前致歉。
谢谢!
可能重复:
Appending the same string to a list of strings in Python.
简而言之-
urls = ['https://amazon.com.au'+link for link in links]
我正在尝试从 Amazon 上删除链接。我运行陷入了一个小问题。
我希望将特定字符串 'a' 添加到列表中的每个字符串。
links = soup.find_all("a", attrs= {"class":"a-link-normal a-text-normal"})
urls = [item.get('href') for item in links]
print(urls)
输出为:
['/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']
现在如何在列表输出中的每个字符串前面添加一个 = https://amazon.com.au?
我希望输出如下:
['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']
我是新手(进入 python 1.5 个月),如果我不够清楚,请提前致歉。
谢谢!
可能重复: Appending the same string to a list of strings in Python.
简而言之-
urls = ['https://amazon.com.au'+link for link in links]