Selenium 中的网页抓取循环

问题描述

我需要抓取此页面中的所有产品:website

所以我需要点击每张照片,然后抓取其中的数据。

我设法编写了用于抓取内部数据的脚本。

我必须提取名称、价格、描述...

下面是我的代码:

import scrapy
from scrapy_selenium import SeleniumRequest
from scrapy.selector import Selector
from selenium.webdriver.common.keys import Keys
from scrapy_splash import SplashRequest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which

class AsoswomennewSpider(scrapy.Spider):
    name = 'asoswomennew'
    allowed_domains = ['www.asos.com']
    start_urls = ['https://www.asos.com/monki/monki-lisa-cropped-vest-top-with-ruched-side-in-black/prd/23590636?colourwayid=60495910&cid=2623']


def __init__(self):
    chrome_option = Options()
    chrome_option.add_argument("--headless")
    chrome_path = which("chromedriver")
    driver = webdriver.Chrome(executable_path=chrome_path,options = chrome_option)
    driver.set_window_size(1920,1080)
    driver.get('https://www.asos.com/monki/monki-lisa-cropped-vest-top-with-ruched-side-in-black/prd/23590636?colourwayid=60495910&cid=2623')


def parse(self,response):

           yield{
           'name':response.xpath("//div[@class='product-hero']/h1/text()").get(),'price':response.css('//*[contains(@class,"current-price")]').get(),'description':response.xpath("//div[@class='product-description']/ul/li/span/text()").getall(),'about_me':response.xpath("//div[@class='about-me']/p/text()").getall(),'brand_description':response.xpath("//div[@class='brand-description']/p/text()").getall()
        }

现在我需要遍历每张图片,然后执行上面的脚本。

pictures to loop

有人可以帮我吗?

谢谢!

PS 我的 start_url 需要更改为这个 'https://www.asos.com/women/new-in/new-in-clothing/cat/?cid=2623&nlid= ww|new+in|new+products|服装'

因为这是主(主页)网页,所以我需要为每个项目设置一个回调网址。

解决方法

我可以看到产品被包裹在一个 article 标签中。

并且每个 article 标签都有一个 a 标签,它基本上由指向该产品的链接组成。

您可以抓取主页中每个 a 标签中的 article 标签并将它们存储在 list 中。假设 list 名称为 products_list

driver.get() 之后是这样的:

products_list = driver.find_elements_by_css_selector('article a')

然后从列表中提取每个 href 标签的 a 值并将它们存储在另一个名为 listproducts_links

products_links = []
for each in products_list:
    products_links.append(each.get_attribute('href'))

现在,您所要做的就是遍历 products_links 并打开每一个,解析您需要的数据。就像你对单个产品所做的一样

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...