【问题标题】:webscraper script continuously runs and but with no output/errorwebscraper 脚本连续运行,但没有输出/错误
【发布时间】:2021-09-09 08:10:11
【问题描述】:

我正在抓取这个site,我试图抓取的数据是产品图片、产品名称、价格和发布日期。

import requests
import time
from bs4 import BeautifulSoup
from discord_webhook import DiscordWebhook, DiscordEmbed

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'}
url = "https://www.adidas.com.sg/release-dates"
productsource = requests.get(url,headers=headers,timeout=15)
productinfo = BeautifulSoup(productsource.text, "lxml")

    #webscraper
for item in productinfo.select(".plc-product-card___1tVAm plc-product-card-mobile___3bmHS"):

    pname = item.find("div", class_='plc-product-name___2cofu gl-product-card__name gl-label gl-label--m gl-label--condensed').get_text(strip=True)                #product title
    price = item.find("div", class_="gl-price-item notranslate gl-label--m").get_text(strip=True)                      #product price
    imagelink = item.find('img')['src']                           #product image link
    plink = f"https://www.adidas.com.sg/{item.a['href']}"                                         #to get product page link
    pdate = item.find("div",class_="gl-price-item notranslate gl-label--m").get_text()


    print(pname,'\n',price,'\n',imagelink,'\n',plink,'\n',pdate)

运行此脚本时,它不会返回任何结果或错误,终端只是空白并且不会停止运行。不太清楚这里有什么问题。谁能指出我正确的方向?

【问题讨论】:

  • 您对get_text 的最终调用需要括号:.get_text()。事实上,pdate 将是一个函数,而不是一个字符串。
  • 哎呀,我在复制代码时错过了它。感谢您让我知道我编辑了它。
  • @meliz 是否正确productinfo.select(".plc-product-card___1tVAm plc-product-card-mobile___3bmHS") 请检查!

标签: python web-scraping beautifulsoup python-requests webautomation


【解决方案1】:

查看网页和代码,你犯了两个错误:

  1. 您循环访问了错误的 <div>
  2. find 函数中使用的语法错误。

解决方案:

for item in productinfo.find('div', {'class': 'plc-product-list___1Lg2h'}):
pdate = item.find("div",class_="gl-price-item notranslate gl-label--m").get_text()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多