Javascript &引用;属性错误:';非类型';对象没有属性';获取文本'&引用;

Javascript &引用;属性错误:';非类型';对象没有属性';获取文本'&引用;,javascript,python,Javascript,Python,每当我尝试运行此代码时: page = requests.get(URL, headers = headers) soup = BeautifulSoup(page.content, 'html.parser') title = soup.find(id="productTitle").get_text() price = soup.find(id="priceblock_ourprice").get_text() converted_price = price[0:7] if (conv

每当我尝试运行此代码时:

page = requests.get(URL, headers = headers)
soup = BeautifulSoup(page.content, 'html.parser')

title = soup.find(id="productTitle").get_text()
price = soup.find(id="priceblock_ourprice").get_text()
converted_price = price[0:7]


if (converted_price < '₹ 1,200'):
    send_mail()
print(converted_price)
print(title.strip())
if(converted_price > '₹ 1,400'):
    send_mail()
page=requests.get(URL,headers=headers)
soup=BeautifulSoup(page.content,'html.parser')
title=soup.find(id=“productTitle”).get_text()
price=soup.find(id=“priceblock\u ourprice”).get\u text()
换算价格=价格[0:7]
如果(换算价格<'₹ 1,200'):
发送邮件
打印(换算价格)
打印(title.strip())
如果(换算价格)>'₹ 1,400'):
发送邮件

它给了我一个错误
AttributeError:“NoneType”对象没有属性“get_text”
前面的代码运行正常。

您查询的页面中不存在
productTitle
id或
priceblock\u ourprice
id。我建议您采取以下两个步骤: -检查浏览器上的URL并查找该ID -检查您在
页面中获得的内容。内容
,因为它可能与您在浏览器中看到的内容不同


希望能有所帮助

我想你是在分析亚马逊的产品吧。 元素
productTitle
priceblock\u ourprice
存在(我已检查)

您应该检查
页面内容
。
可能您的
标题不适用于网站

尝试:

结果:

COMIFORT, Computerschreibtisch, Schreibtisch für das Arbeitszimmer, Schreibtisch, Maße: 90 x 50 x 77 cm                         
50,53 €

我试过了,效果很好,请检查一下,这可能是因为产品缺货意味着价格不在网站上,这就是为什么它没有类型。尝试选择其他价格可见的产品。

UI可能会更改。ID:
priceblock\u ourprice
可能在新UI中不再可用。要么
soup.find(ID=“productTitle”)
要么
soup.find(ID=“priceblock\u ourprice”)
返回
欢迎使用堆栈溢出!请正确格式化您的代码。虽然此代码可能会解决问题,但如何以及为什么解决此问题将真正有助于提高您的文章质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
COMIFORT, Computerschreibtisch, Schreibtisch für das Arbeitszimmer, Schreibtisch, Maße: 90 x 50 x 77 cm                         
50,53 €
import requests



from bs4 import BeautifulSoup 

url = 'https://www.amazon.com/Camera-24-2MP-18-135mm-Essential-Including/dp/B081PMPPM1/ref=sr_1_1_sspa?dchild=1&keywords=Canon+EOS+80D&qid=1593325243&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEyU1M0M1JVTkY3WTBVJmVuY3J5cHRlZElkPUEwNDQzMjI5Uk9DM08zQkM1RU9RJmVuY3J5cHRlZEFkSWQ9QTAyNjI0NjkzT0ZLUExSRkdJMDYmd2lkZ2V0TmFtZT1zcF9hdGYmYWN0aW9uPWNsaWNrUmVkaXJlY3QmZG9Ob3RMb2dDbGljaz10cnVl'

headers = { "user-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}

page = requests.get(url,headers= headers)

soup = BeautifulSoup(page.content,"lxml")

title = soup.find(id = "productTitle").get_text()

print(title)