我正在尝试从Tesco网站上抓取数据,以获取产品的名称和价格。下面是我的代码。一些产品没有价格,因为它们已经卖完了,Python给我一个错误,因为没有东西要刮。我希望它能够跳过该磁贴并在没有价格的情况下移至下一个磁贴。
有人知道我该怎么做吗?
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'):
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)
使用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
您还可以通过以下方式使它更具交互性:
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'):
name =name_of.h3.a.text
print(name)
try:
price = name_of.find('div', class_='price-details--wrapper')
pricen =price.find('span', class_='value').text
print(pricen)
except:
print('Sold Out')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句