0

I'm trying to display the elements of a dictionary but I'm having difficulties, when I try to display it shows the whole object, this is my code:

produtos = []
@tasks.loop(seconds=5)
async def aviso():
 request = requests.get("https://www.nike.com.br/snkrs#estoque")
 soup = bs4(request.text, "html.parser")
 links = soup.find_all("a", text="Comprar", class_="btn")
 for link in links:
  produtos.append(link["href"])
 print(produtos)

The problem here is that it displays everything in this format:

['https://www.nike.com.br/air-max-90-153-169-211-330199', ...]

How can I get only the link on this dictionary?

Edit:

I want to display it this way:

'https://www.nike.com.br/air-max-90-153-169-211-330199', ...

I'm creating a bot for discord, I posted only the useful part of the code.

  • `produtos.append(link["href"])` means you are starting off with a list and populating it with a dictionary result. If the length of links is greater than 1, you're going to get more than 1 link result populating in that list, so you need to decide how you best want to approach this. You can access the first link with `produtos[0]` for instance – PeptideWitch Jun 23 '21 at 22:59
  • What's wrong with that output? That's just the link. – Barmar Jun 23 '21 at 23:00
  • @PeptideWitch Plus I want to get all the links, no square brackets [ –  Jun 23 '21 at 23:04
  • @Barmar I want to get the links without the square brackets –  Jun 23 '21 at 23:05
  • 2
    Confusion would be avoided if you showed what you wanted the result to be. – Barmar Jun 23 '21 at 23:09
  • @Barmar I want to be able to print the links, as a string, but without the square brackets, and with a comma, as I mentioned in the question, but without square brackets –  Jun 23 '21 at 23:12
  • You need to say that in the question, not a comment. It's not clear. When I read the question, I thought you meant that it was showing other items from the dictionary in addition to the `href`. – Barmar Jun 23 '21 at 23:18
  • Show an example of the desired output, because saying it in words isn't always clear. – Barmar Jun 23 '21 at 23:18
  • @Barmar I already edited –  Jun 23 '21 at 23:21
  • BTW, the dictionary is irrelevant to the question. You just want to know how to display a list of strings without the square brackets. It doesn't matter that the strings originally came from a list of dictionaries. – Barmar Jun 23 '21 at 23:23
  • @Barmar Actually it's important that the links are in a dictionary, I'll use this variable at the front of my script –  Jun 23 '21 at 23:27
  • It's important for your application, but it has nothing to do with the specific problem you're having. – Barmar Jun 23 '21 at 23:31
  • SO once again, the question linked as a duplicate of doesn't satisfy the OP's requirement. He wants quote around the final output – Mab Jun 23 '21 at 23:32

2 Answers2

1

Concatenate the representations of the links, separated by comma:

print(", ".join(map(repr,produtos)))

repr() will put quotes around the link, and ", ".join() will separate them with comma.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I need to get the link from the dictionary, because I need to get all the links, not one by one, and also because I'm going to use it later in my code –  Jun 23 '21 at 23:08
  • By the way, I declared the variable as global, I forgot to add the question –  Jun 23 '21 at 23:08
  • I've updated the answer based on your comment above. – Barmar Jun 23 '21 at 23:08
0

To print the entries in produtos without the brackets, simply do print(*produtos, sep=", ") In this case set sep to whatever you want to be the separator between the links

EDIT To have quote around the final output

print(*("'{}'".format(item) for item in produtos), sep=', ')
Mab
  • 412
  • 3
  • 12
  • This displays without the square brackets, the problem is that the links are not enclosed in quotes, and there is no comma, how can I fix this? –  Jun 23 '21 at 23:06
  • If you want comma as the *separator* then that's ```print(*produtos, sep=", ")``` as I said in my answer. Set **sep** to whatever you want to separate the links with – Mab Jun 23 '21 at 23:09
  • That's what I did, the problem is that the links are still not in quotation marks –  Jun 23 '21 at 23:13
  • It now display quotes, I updated my answer – Mab Jun 23 '21 at 23:23