【问题标题】:printing same name, price,and link in BeautifulSoup python在 BeautifulSoup python 中打印相同的名称、价格和链接
【发布时间】:2021-03-01 11:47:20
【问题描述】:

如何获取所有产品详细信息,它打印相同的内容,但我希望其他产品也详细说明 这是我要获取所有产品数据的链接: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)

【问题讨论】:

    标签: python selenium beautifulsoup python-requests


    【解决方案1】:

    会发生什么?

    1. 您的print 缩进错误
    2. 它们只是classproduct-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)
    

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2021-05-26
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多