from bs4 import BeautifulSoup
import requests
murl = ['https://www.amazon.in/dp/B07XY541GH/','https://www.amazon.in/dp/B085J17VVP/']
def track(url):
req = requests.get(url)
soup = BeautifulSoup(req.content, 'html5lib')
pr = soup.find('span', id='priceblock_ourprice').getText()
con_pr = pr[1:]
converted_price = con_pr.strip()
newprice = ''
for con in converted_price:
if con != ',':
newprice = newprice + con
newprice = float(newprice)
return newprice
def main():
for url in murl:
price = track(url)
print(price)
main()
当代码第二次循环时,我似乎总是这样。即使我将url存储在两个不同的变量中并逐个调用函数,我仍然会得到相同的错误
2990.0
Traceback (most recent call last):
File "test.py", line 28, in <module>
main()
File "test.py", line 25, in main
price = track(url)
File "test.py", line 13, in track
pr = soup.find('span', id='priceblock_ourprice').getText()
AttributeError: 'NoneType' object has no attribute 'getText'
有什么解决办法吗
以下是解决方案,请尝试:
相关问题 更多 >
编程相关推荐