无论我做什么,亚马逊id对象都将返回None。作为一个实验,我在ebay id对象上尝试了这个确切的代码,并且有效。亚马逊有什么不同?我也已经尝试将html.parser更改为lxlm,并且仍然返回:
AttributeError:“ NoneType”对象没有属性“ get_text”
可以在getPrice()def中找到问题
from bs4 import BeautifulSoup
import time
import smtplib
URL = 'https://www.lego.com/en-us/product/darth-vader-s-castle-75251'
headers = {'Users-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36'}
wanted = 80
email = "help@gmail.com"
password = 'password'
Server_name = 'mail.gmail.com'
MAIL_USE_SSL=True
def sendMail():
subject = 'Ebay Price has Dropped!!'
mailtext = "Subject:"+subject+"\n\n"+URL
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
server.ehlo()
server.starttls()
server.login(email,password)
server.sendmail(email,email,mailtext)
print("Sent Email")
pass
def trackPrice():
price = getPrice()
if price > wanted:
diff = (price - wanted)
diff = round(diff,5)
print(f"it's still ${diff} over-priced")
else:
print('cheaper')
sendMail()
def getPrice():
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content,"html.parser")
price = soup.find(id="priceblock_ourprice").get_text().strip()[4:]
price = float(price)
print(price)
return price
if __name__ == "__main__":
while True:
trackPrice()
time.sleep(100)
假设您的实际网址是这样的:
Then if you print the
soup
variable you will see that Amazon has detected that you are trying to scrape their page and presented you an error page instead, since the content starts with:This explains why the HTML tag with
id="priceblock_ourprice"
is not found,find(...)
returnsNone
and theget_text()
function fails.