我正试图从Tesco网站上抓取数据,以获取产品的名称和价格。下面是我的代码。一些产品没有价格,因为它们已经卖完了,Python给我一个错误,因为没有东西要刮。我希望它能够跳过该磁贴,并在价格不可用时转到下一个磁贴。
有人知道我该怎么做吗?
2 3
4 5
6 7
答案 0 :(得分:0)
使用.close-button{
float: right;
top:-24px;
right:-24px;
}
-try
块:
except
您还可以通过以下方式提高互动性:
from bs4 import BeautifulSoup
import requests
#URL to be scraped
url_to_scrape = 'https://www.tesco.com/groceries/en-GB/shop/fresh-food/all?page=1&count=48'
#Load html's plain data into a variable
plain_html_text = requests.get(url_to_scrape)
#parse the data
soup = BeautifulSoup(plain_html_text.text, "lxml")
#Get the name of the class
for name_of in soup.find_all('div',class_='product-tile-wrapper'):
try:
name =name_of.h3.a.text
print(name)
price = name_of.find('div', class_='price-details--wrapper')
pricen =price.find('span', class_='value').text
print(pricen)
except:
pass