python - 代码第二次循环时bs4出错
问题描述
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'
有什么解决办法吗?
解决方案
这是解决方案,试试看:
import requests
from bs4 import BeautifulSoup
murl = ['https://www.amazon.in/dp/B07XY541GH/', 'https://www.amazon.in/dp/B085J17VVP/']
def track(url):
req = requests.get(url, headers={
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.87 Safari/537.36"})
soup = BeautifulSoup(req.content, 'html5lib')
pr = soup.find('span', id=lambda x: x and x.startswith('priceblock_')).text
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()
- 使用错过的 user-aget。大多数情况下它应该用于解析
- 元素标签对象没有 getText() 方法
- 两个站点的 ID 不同
推荐阅读
- sql-server - 动态 SQL 中的 SQL 替换调用
- c++ - 在来自另一个线程未定义行为的销毁调用期间调用对象上的方法吗?
- r - 如何在 R Studio IDE 中前进到下一次出现的变量
- javascript - 如何将我的 ajax 数据添加到 bootstrap4 卡
- javascript - 如何删除织物画布箭头?
- swagger - Swagger:在 Swagger UI 中更改 api 路由
- java - 在 AlertDialog 中,如何将图像设置在顶部
- laravel-5 - 显示存在关系的所有条目(雄辩)
- jenkins - 在 workflow-cps groovy 代码中获取 CpsScript 实例?
- json - Flutter - 使用 API 密钥