Skip to content

Instantly share code, notes, and snippets.

@shahidcsebd
Created December 15, 2021 05:22
Show Gist options
  • Save shahidcsebd/d07dd7de7f3254f8d99023fc0a6a79b8 to your computer and use it in GitHub Desktop.
Save shahidcsebd/d07dd7de7f3254f8d99023fc0a6a79b8 to your computer and use it in GitHub Desktop.
Search on Amazon with a Keyword and Get the Result Page
import requests
from bs4 import BeautifulSoup
search_keyword = input("Write a keyword to search on Amazon: ")
search_format = search_keyword.strip().replace(' ', '+')
make_url = 'https://www.amazon.com/s?k=' + search_format
print(make_url)
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36", "Referer": "https://www.amazon.com/"}
response = requests.get(make_url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# Print the nicely formatted source code
print(soup.prettify())
links = soup.findAll('a')
# Print All links in the amazon SERP
for link in links:
print(link.get('href'))
# Now I need to scrap the first 10 product URLs from this soup. Please help...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment