python - 如何在 Scrapy 中使用正则表达式

标签 python scrapy

我想废弃 website 并提取商品的以下价格,但同一商品有多个价格,我如何正确过滤出商品的价格..

以下是解析 page=1 上商品价格的代码:

hxs.select("//div[@class='category-products']/ul/li//div[@class='price-box']//span[@class='price']").extract()

这给出:

[u'<span class="price" id="old-price-8963">\n                    \u20b9 8,990                </span>',
 u'<span class="price" id="product-price-8963">\n                    \u20b9 5,990                </span>',
 u'<span class="price" id="old-price-8940">\n                    \u20b9 8,990                </span>',
 u'<span class="price" id="product-price-8940">\n                    \u20b9 5,990                </span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 2,990</span>']

前两个价格是相同的商品......第二个商品的价格也类似......但其余的只有一个价格......任何人都可以建议我一种解决这个问题的方法......

最佳答案

l = [u'<span class="price" id="old-price-8963">\n                    \u20b9 8,990                </span>',
 u'<span class="price" id="product-price-8963">\n                    \u20b9 5,990                </span>',
 u'<span class="price" id="old-price-8940">\n                    \u20b9 8,990                </span>',
 u'<span class="price" id="product-price-8940">\n                    \u20b9 5,990                </span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 7,990</span>',
 u'<span class="price">\u20b9 2,990</span>']
>>> s =set()
>>> for x in l:
    import re
    if not 'old-price' in x:
        m = re.match('<span[^>]*>([^<]*)</span>', x)
        if m is None:
            print x
        s.add(m.group(1).strip())


>>> s
set([u'\u20b9 5,990', u'\u20b9 2,990', u'\u20b9 7,990'])

希望你能继续下去!

关于python - 如何在 Scrapy 中使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16568307/

相关文章:

python - 项目的结构化日志记录

python - WIN32 程序可以使用 MYSQL 通过 Django 身份验证系统进行身份验证吗?

java - 在 Java 中使用 Scrapy?

python - 允许使用 Scrapy Image Pipeline 进行重复下载吗?

python - scrapy:尝试调试代码的新手

Python、Scrapy、 Selenium : how to attach webdriver to "response" passed into a function to use it for further action

python - 快速 API - 如何在 GET 中显示来自 POST 的图像?

python - 使用 csvImporter 将 csv 导入 django 模型

python - 没有外键约束的连接

python - 在scrapy爬行中使用css选择器提取span之外的文本