Daniel
Daniel

Reputation: 191

Code prints out only first item searched instead of all of them

Here is the entire code:

from bs4 import BeautifulSoup
import requests
import lxml

search_item = ["Sony Camera"]

def all_links(names):
    url = "https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2380057.m570.l1312.R1.TR11.TRC2.A0.H0.XIp.TRS1&_nkw="
    urls = []
    for name in names:
             urls.append(url + name.replace(" ", "+"))
    return urls

def display_results(urls):
    for url in urls:
        res = requests.get(url)
        res.raise_for_status()
        soup = BeautifulSoup(res.text, 'lxml')
        name = soup.findAll("h3", {"class": "s-item__title"}).get_text(separator=u" ")
        price = soup.findAll("span", {"class": "s-item__price"}).get_text()

        print("Item Name: " + name + "\n")
        print("Price: " + price + "\n")

display_results(all_links(search_item))   

Not sure why am I getting only first item found printed out. The lines code for name and price must be culprit. Thanks

Upvotes: 0

Views: 82

Answers (1)

baduker
baduker

Reputation: 20042

You have to loop thru the .find_all() to get all the items, because now, you're only getting the first result only with the [0].

By the way, indentation in Python is crucial. Typically, 4 spaces are used, not 6 as in your example. And you have an unused import lxml.

Here's how:

import locale

import requests
from bs4 import BeautifulSoup

base_url = "https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2380057.m570.l1312.R1.TR11.TRC2.A0.H0.XIp.TRS1&_nkw="
search_item = ["Sony Camera"]

locale.setlocale(locale.LC_ALL, "en_US.UTF-8")


def all_links(names):
    return [f"{base_url}{name.replace(' ', '+')}" for name in names]


def display_results(urls):
    for url in urls:
        res = requests.get(url)
        res.raise_for_status()
        soup = BeautifulSoup(res.text, "html.parser")
        names = [
            i.getText(strip=True) for i
            in soup.find_all("h3", {"class": "s-item__title"})
        ]
        prices = [
            i.getText(strip=True).replace("$", "") for i
            in soup.find_all("span", {"class": "s-item__price"})
        ]

        prices = [locale.atof(p) for p in prices]
        product_data = sorted(zip(names, prices), key=lambda t: t[1], reverse=True)

        for product_info in product_data:
            product_name, product_price = product_info
            print(f"Item Name: {product_name}\nPrice: {product_price}")


display_results(all_links(search_item))

Output:

Item Name: New ListingSony a7 III 24.2 MP Mirrorless Digital Camera - Black (Body Only)
Price: 1450.0
Item Name: Sony Cyber-shot RX10 IV 20.2 MP Digital Camera DSC-RX10 IV Kit
Price: 1275.0
Item Name: Sony Alpha a6400 Mirrorless Camera
Price: 950.0
Item Name: Sony Alpha A7 Full Frame Mirrorless Camera
Price: 800.0
Item Name: [RARE! UNUSED in BOX] Sony SLT-A65VX  24.3MP DSLR Camera Double Lens Kit From JP
Price: 799.99

and so on...

Upvotes: 1

Related Questions