from bs4 import BeautifulSoup
import requests
auctionurl = requests.get('https://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=p2334524.m570.l1313&_nkw=logitech&_sacat=0&LH_TitleDesc=0&LH_Auction=1&rt=nc&_odkw=Logitech&_osacat=0&LH_ItemCondition=3').text
soupauction = BeautifulSoup(auctionurl, 'lxml')
logitechauction = soupauction.find('li', class_ = "s-item s-item__pl-on-bottom s-item--watch-at-corner")
iteminfo = logitechauction.find('div', class_ = 's-item__details clearfix')
shipping = iteminfo.find('span', class_ = 's-item__shipping s-item__logisticsCost').text
name = logitechauction.find('h3', class_ = 's-item__title').text
price = iteminfo.find('span', class_ = 's-item__price').text
print(name, price, shipping)
soldurl = requests.get('https://www.ebay.co.uk/sch/i.html?_from=R40&_nkw=logitech&_sacat=0&LH_TitleDesc=0&LH_ItemCondition=3&LH_Sold=1&LH_Complete=1&rt=nc&LH_All=1').text
soupsold = BeautifulSoup(soldurl, 'lxml')
logitechsold = soupsold.find('li', class_ = 's-item s-item__pl-on-bottom')
soldinfo = logitechsold.find('div', class_ = 's-item__info clearfix')
soldname = soldinfo.find('h3', class_ = 's-item__title s-item__title--has-tags').text
soldprice = soldinfo.find('span', class_ = 's-item__price').text
print('Sold =', soldname, soldprice)
It prints the first "print(name, price, shipping)", but then the error happens on line 22 I get the error
Traceback (most recent call last):
File "c:\Users\zackz\Desktop\EbaySniping.py", line 22, in <module>
soldname = soldinfo.find('h3', class_ = 's-item__title s-item__title--has-tags').text
AttributeError: 'NoneType' object has no attribute 'text'
When i remove the .text at the end of the line i just get
Sold = None $20.00
Which isnt the right data that it should be.
CodePudding user response:
The first <li >
doesn't contain anything, so skip it. You can do this by using .find_all
and then get tag at second index ([1]
):
...
# the first <li > doesn't contain desired value, so skip it:
logitechsold = soupsold.find_all("li", class_="s-item s-item__pl-on-bottom")[1]
soldinfo = logitechsold.find("div", class_="s-item__info clearfix")
soldname = soldinfo.find(
"h3", class_="s-item__title s-item__title--has-tags"
).text
soldprice = soldinfo.find("span", class_="s-item__price").text
print("Sold =", soldname, soldprice)
Prints:
Logitech Streamcam 60fps 1080p *NEW* £50.00 £16.92 postage estimate
Sold = Logitech G Pro USB 8LO920009426 Mechanical Gaming Keyboard - Black £50.40