从Scrapy结果中删除/排除非中断空格

时间:2016-06-24 09:56:45

标签: python scrapy

我目前正试图抓住网站上的文章价格,但我遇到了一个问题(在某种程度上解决了价格动态生成的问题,这是一个巨大的痛苦)。

我能够毫无问题地收到价格和文章名称,但每一秒的结果都是“价格”和“价格”。是" \ xa0"。我尝试使用' normalize-space()'删除它。但无济于事。

我的代码:

import scrapy
from scrapy import signals
from scrapy.http import TextResponse
from scrapy.xlib.pydispatch import dispatcher
from horni.items import HorniItem

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.keys import Keys

class mySpider(scrapy.Spider):
    name = "placeholder"
    allowed_domains = ["placeholder.com"]
    start_urls = ["https://www.placeholder.com"]

    def __init__(self):
        self.driver = webdriver.Chrome()
        dispatcher.connect(self.spider_closed, signals.spider_closed)

    def spider_closed(self, spider):
        self.driver.close()

    def parse(self, response):
        self.driver.get("https://www.placeholder.com")
        response = TextResponse(url=self.driver.current_url, body=self.driver.page_source, encoding='utf-8')
        for post in response.xpath('//body'):
            item = myItem()
            item['article_name'] = post.xpath('//a[@class="title-link"]/span/text()').extract()
            item['price'] = post.xpath('//p[@class="display-price"]/span]/text()').extract()
            yield item

1 个答案:

答案 0 :(得分:4)

\xa0是Latin1中的一个不间断的空间。像这样替换它:

string = string.replace(u'\xa0', u' ')

更新

您可以按以下方式应用代码:

for post in response.xpath('//body'):
    item = myItem()
    item['article_name'] = post.xpath('//a[@class="title-link"]/span/text()').extract()
    item['price'] = post.xpath('//p[@class="display-price"]/span]/text()').extract()
    item['price'] = item['price'].replace(u'\xa0', u' ')
    if(item['price'].strip()):
        yield item

在这里你替换了char,然后只在价格不为空时才产生该项目。