r/CodingHelp • • Jan 03 '22

Python: Amazon web scraper has suddenly stopped working, after changing nothing

I am a 14 year old who is learning python and I am making a simple amazon web scraper to collect the title and price of all the search results (in this case a camera). I am using Beautiful Soup 4 and Requests. After many hours of work, I finished with the work below, which was working. However, after doing something else for a few minutes (unrelated to code), I came back to the program to find it stopped working. Now, the links in the scrapeResults function returned an empty list, despite me not changning anything. This has broken the program, and I cant fix it. Any help would be appreciated.

Code:

from bs4 import BeautifulSoup
from time import sleep
import requests


def makeSoup(url):
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"}
    loop = True
    while loop:
        try:
            page = requests.get(url, headers=headers)
            loop = False
        except:
            print("Connection refused by the server.")
            print("Sleeping for 5 seconds...")
            sleep(5)
            continue

    soup = BeautifulSoup(page.content, "lxml")
    return soup


def scrapeResults(soup):
    links = soup.find_all("a", class_="a-link-normal s-link-style a-text-normal")
    print(links)
    for link in links:
        href = link.get("href")
        url = "https://www.amazon.co.uk" + href
        s = makeSoup(url)
        data = scrapePage(s)
        print(data)


def scrapePage(soup):
    try:
        title = soup.select_one("#productTitle").text.strip()
    except:
        title = ""
    try:
        price = soup.find("span", class_="a-offscreen").text.strip()
    except:
        price = ""
    data = {"title": title, "price": price}
    return data


def main():
    NAME = "camera"
    URL = "https://www.amazon.co.uk/s?k=" + NAME
    soup = makeSoup(URL)
    scrapeResults(soup)


if __name__ == "__main__":
    main()
15 Upvotes

13 comments sorted by

View all comments

1

u/creepy_kittycat Jan 03 '22

Have you checked wehther the html of the target site hasn't changed since you finalised the code last? Some websites use dynamic html tags, meaning the tag and/or class strings change every time you visit the site for example. This would be my first guess. Good luck!

2

u/Hypernova_13 Jan 03 '22

Im fairly certain that it matched up but incase it had changed how would I overcome this?

1

u/creepy_kittycat Jan 03 '22

I haven't found a decent sollution to that, apart from completely changing the strategy and working with Selenium instead of bs4. It's a challenging rabbit hole, I think, but it's not necessarily going to be more reliable. If Amazon has an API, it'd be worth checking into. But in case it doesn't, I don't know what else to say other than keep checking the html (if that's the problem). Although.... I notice in your scrapeResults function you don't have a return statement, but maybe this is by design? In any case, when it comes to scrapers, I'd sleep a night on it and then go back to the problem with fresh eyes and mind :)

1

u/Hypernova_13 Jan 03 '22

I actually had some trouble with accidentally undoing parts of my code so the return statement might have been undone by accident

1

u/creepy_kittycat Jan 04 '22

that's fair, but you still have a print statement so if that's showing to be empty there's still an unrelated issue in there, no?