首页 > 解决方案 > 在 BeautifulSoup python 中打印相同的名称、价格和链接

问题描述

如何获取所有产品详细信息它打印相同的内容,但我希望其他产品也详细说明这里是我要获取所有产品数据的链接:https ://www.nike.com/gb/w/womens -lifestyle-shoes-13jrmz5e1x6zy7ok

import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from selenium import webdriver


url= "https://www.nike.com/gb/w/womens-lifestyle-shoes-13jrmz5e1x6zy7ok"
driver = webdriver.Chrome('D:/chromedriver')
driver.get(url)
pageSource = driver.page_source
for n in range(10):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    soup = BeautifulSoup(pageSource, 'lxml')
    content= soup.findAll('div',class_='product-grid')
    content 
    for item in content:
        title= item.find('div',class_ = 'product-card__title').text
        link = item.find('a', {'class': 'product-card__link-overlay'})['href']
        price=item.find('div',class_ ='product-price css-11s12ax is--current-price').text
    print(title,price,link)

标签: pythonseleniumbeautifulsouppython-requests

解决方案


怎么了?

  1. 他们是你的错误缩进print
  2. 它们只是一个class元素product-grid

怎么修?

  1. 检查你的缩进print,它应该在循环中打印项目。

  2. 将您的选择更改为:

    content= soup.find_all('div',class_='product-card')
    
  3. 奖励:findAll()最好使用新语法find_all()

例子

import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from selenium import webdriver


url= "https://www.nike.com/gb/w/womens-lifestyle-shoes-13jrmz5e1x6zy7ok"
driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')
driver.get(url)
pageSource = driver.page_source
for n in range(10):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    soup = BeautifulSoup(pageSource, 'lxml')
    content= soup.find_all('div',class_='product-card')
    content 
    for item in content:
        title= item.find('div',class_ = 'product-card__title').text
        link = item.find('a', {'class': 'product-card__link-overlay'})['href']
        price=item.find('div',class_ ='product-price css-11s12ax is--current-price').text
        print(title,price,link)

推荐阅读