Trying to use bs4 to skip an attribute if there is no data

Viewed 248

I'm trying to write a program that will pull data from a URL and format it so that I can copy into another program. I've got everything working except I can't get it to skip an item if there is no img src in the imagelink tag.

import requests, sys, webbrowser, bs4
res = requests.get('http://hzws.selco.info/prototype.php?type=new-arrivals&lib=nor&collect=Bnewnf,Bnewmys,Bnewf,Bnewsf&days=14&key=7a8adfa9aydfa999997af')
res.raise_for_status()

soup = bs4.BeautifulSoup(res.text, "lxml")

img = soup.select('imagelink') #why won't this pull anything?!?!?!?!
link = soup.select('cataloglink')

length = min([14, len(img)])
for i in range(length):
  img1 = img[i].getText()
  link1 = link[i].getText()
  print('<div>' + link1 + img1 + '</a></div>')

Right now this prints all of the URLs regardless of whether or not there is an imagelink attached to it. I've tried numerous different things to get it to skip an item if there is no img src. Any ideas?

1 Answers
Related