Issue: the “linkElems” list appears to be empty
Suspect what is causing the issue: I think the tags I’m telling it to grab is wrong
Function of Program:
- Search Amazon.com for arguments in command line and download the website into the variable “res”
- Select the URLs for the links of the results of the search and store them into a list called “linkElems”
- Open new browser tabs for the first 5 results
Context: I’ve finished Chapter 11 of Automate the Boring stuff and and using the same code from the first project except I’ve tweaked it a little bit to search Amazon search results instead of google.
What Tags I’ve tried:
- 'a'
- ‘h2. a’
- 'a.a-link-normal a-text-normal'
- '.h2 a'
#! python3
#Shop on Amazon - searchs amazon and opens the first 5 top results
import sys,requests,bs4,webbrowser,logging
print ('Searching')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'
}
res = requests.get('https://www.amazon.com/s?k=' + ''.join(sys.argv[1:]))
res.raise_for_status
soup = bs4.BeautifulSoup(res.text,features = 'html.parser')
linkElems = soup.select('a.a-link-normal a-text-normal')
numOpen = min(5, len(linkElems))
for i in range(numOpen):
webbrowser.open('https://amazon.com' + linkElems[i].get('href'))
HTML example of link I'm trying to grab using the tags: