Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How Can I Assign A Variable To All Of The Items In A List?

I’m following a guide and it’s saying to print the first item from an html document that contains the dollar sign.

It seems to do it correctly, outputting a price to the terminal and it actually being present on the webpage. However, I don’t want to have just that single listing, I want to have all of the listings and print them to the terminal.

I’m almost positive that you could do this with a for loop, but I don’t know how to set that up correctly. Here’s the code I have so far with a comment on line 14, and the code I’m asking about on line 15.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from bs4 import BeautifulSoup
import requests
import os

os.system("clear")

url = 'https://www.newegg.com/p/pl?d=RTX+3080'

result = requests.get(url)
doc = BeautifulSoup(result.text, "html.parser")

prices = doc.find_all(text="$")

#Print all prices instead of just the specified number?
parent = prices[0].parent


strong = parent.find("strong")
print(strong.string)

>Solution :

You could try the following:

from bs4 import BeautifulSoup
import requests
import os

os.system("clear")

url = 'https://www.newegg.com/p/pl?d=RTX+3080'

result = requests.get(url)
doc = BeautifulSoup(result.text, "html.parser")

prices = doc.find_all(text="$")

for price in prices:
    parent = price.parent
    strong = parent.find("strong")
    print(strong.string)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading