python - 在 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)
解决方案
怎么了?
- 他们是你的错误缩进
print
- 它们只是一个
class
元素product-grid
怎么修?
检查你的缩进
print
,它应该在循环中打印项目。将您的选择更改为:
content= soup.find_all('div',class_='product-card')
奖励:
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)
推荐阅读
- docker - Docker network ls 使用 --format 仅获取网络名称
- c++ - 是否可以返回对静态或动态指针转换结果的引用?
- python - 如何在 Python 代码中检查气流数据库中是否已存在 Airflow 用户
- javascript - Javascript - 代码结构问题。从“if”语句返回 bool 并在“else if”中重用它
- c - 为什么我得到 free():在 tcache 2 中检测到双重释放并中止(核心转储)*
- python - Flask SQLAlchemy - session.add 违反主键约束
- python-3.x - 节点大小在运行之间变化,而数据在运行之间没有变化
- html - 在这个 mixin 的 @if 语句中,为什么这个大于运算符不起作用?
- javascript - 如何根据选定的单选按钮值传递单选按钮值?
- discord - Discord.JS v12 移至语音通道