簡體   English   中英

Beautiful soup 在使用 class 名稱過濾數據時返回空

[英]Beautiful soup returns empty while using class name to filter datas

我正在嘗試從以下站點的列表中檢索產品名稱、重量和價格。

https://www.licious.in/seafood

我查找產品列表的代碼:

  import requests
  from bs4 import BeautifulSoup

  URL = 'https://www.licious.in/seafood'
  r = requests.get(URL)
  soup = BeautifulSoup(r.content, 'lxml')

  productlist = soup.find_all('div', class_="card")

  productlinks = []

  for card in productlist:
        for link in card.find_all('a', href=True):
  print(link['href'])

我可以使用 css 選擇器來選擇產品價格和產品重量嗎?

我在這里看到幾個問題:

  1. 缺少縮進
  2. 您嘗試使用soup.find_all("div", {"class": "product_image"})獲取的產品圖片是img元素,而不是div
  3. item.find("img")收到的img1 object 不可打印。
    這應該更好用:
import requests,
from bs4 import BeautifulSoup

URL = 'https://www.licious.in/seafood'
r = requests.get(URL)
soup = BeautifulSoup(r.text)
product_list = soup.find_all("div", {"class": "item-details"})

for item in product_list:

    product_names = item.find_all("div", {"class":  "item-title"})
# I see no point where detail_link_h2, detail_link_h3 and detail_link_h4 are defined so this will not work
#print(detail_link_h2 + ", " + detail_link_h3 + ", " + detail_link_h4)

product_images = soup.find_all("img", {"class": "product_image"})

for image in product_images:

    img1 = image.find("img")
#    print(img1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM