我最近了解了网络抓取,并想创建一个程序来抓取每日产品价格。我正在 python 中使用 requests 和 bs4 来抓取 target.com。到目前为止,这是我的代码:
TIMES = [2, 3, 4, 5, 6, 7]
url = 'https://www.target.com/p/dyson-ball-animal-2-upright-vacuum-iron-purple/-/A-52190951'
sleep(choice(TIMES))
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
sleep(choice(TIMES))
name = soup.find('h1').get_text().strip().replace(',', ';')
print('Product name: ', name)
sleep(choice(TIMES))
current_price = soup.find('span', {'data-test': 'product-savings'})
print('Current price: ', current_price)
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,产品名称是正确的,但当前价格始终为“无”。我应该有其他方式来搜索产品价格吗?
提前致谢!