使用Scrapy抓取单个链接 - python

我正在搜寻dior.com的产品。 head / script为我提供了除产品说明以外的所有我需要的字段。要抓取描述,我需要点击链接(以下代码中的url变量)。我熟悉的唯一方法是使用BeautifulSoup。我可以仅使用Scrapy解析吗?
谢谢你们

class DiorSpider(CrawlSpider):
    name = 'dior'
    allowed_domains = ['www.dior.com']
    start_urls = ['https://www.dior.com/en_us/']
    rules = (
        Rule(LinkExtractor(allow=(r'^https?://www.dior.com/en_us/men/clothing/new-arrivals.*',)), callback='parse_file')
    )

    def parse_file(self, response):
        script_text = response.xpath("//script[contains(., 'window.initialState')]").extract_first()
        blocks = extract_blocks(script_text)
        for block in blocks:
            sku = re.compile(r'("sku":)"[a-zA-Z0-9_]*"').finditer(block)
            url = re.compile(r'("productLink":{"uri":)"[^"]*').finditer(block)
            for item in zip(sku, url):
                scraped_info = {
                    'sku': item[0].group(0).split(':')[1].replace('"', ''),
                    'url': 'https://www.dior.com' + item[1].group(0).split(':')[2].replace('"', '')
                }

                yield scraped_info

参考方案

如果您需要从第二个请求中提取其他信息,而不是在第二个请求中提取数据,则应该对包含您已经在Request.meta属性中提取的信息的URL发出请求。

from scrapy import Request

# …

    def parse_file(self, response):
        # …
        for block in blocks:
            # …
            for item in zip(sku, url):
                # …
                yield Request(url, callback=self.parse_additional_information, meta={'scraped_info': scraped_info}

    def parse_additional_information(self, response):
        scraped_info = response.meta['scraped_info']
        # extract the additional information, add it to scraped_info
        yield scraped_info

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

Python:传递记录器是个好主意吗? - python

我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…