我制作了這個小機器人,它通過搜索參數列表進行處理。它工作正常,直到頁面上出現多個結果:product_prices_euros給出一半為空的項目列表。因此,當我與 連接時product_prices_cents,我有如下輸出: 'price' : '',76 對于一半的結果。有沒有一種簡單的方法可以防止收集空物品?我的輸出product_prices_euros看起來像:[' 1', ' ', ' 2', ' ', ' 2', ' ', ' 1', ' ', ' 1', ' ', ' 1', ' ', ' 2', ' ']我只想保留“1”、“2”等...這是看起來像 CSS 的內容。這方面可能有一些東西:< span class="product-pricing__main-price" >2 < span class="cents" >,79€< /span >< /span >還有我的代碼:def start_requests(self): base_url="https://new.carrefour.fr/s?q=" test_file = open(r"example", "r") reader = csv.reader(test_file) for row in reader: if row: url = row[0] absolute_url = base_url+url print(absolute_url) yield scrapy.Request(absolute_url, meta={'dont_redirect': True, "handle_httpstatus_list": [302, 301]}, callback=self.parse)def parse(self, response): product_name = response.css("h2.label.title::text").extract() product_packaging = response.css("div.label.packaging::text").extract() product_price_euros = response.css("span.product-pricing__main-price::text").extract() product_price_cents = response.css("span.cents::text").extract() for name, packaging, price_euro, price_cent in zip(product_name, product_packaging, product_price_euros, product_price_cents): yield { 'ean' : response.css("h1.page-title::text").extract(), 'name': name+packaging, 'price': price_euro+price_cent}任何的想法?:)
2 回答

函數式編程
TA貢獻1807條經驗 獲得超9個贊
如果您只是過濾空的歐元元素,您如何將它們與適當的美分匹配?
首先,恕我直言,我認為如果您遍歷產品以收集其數據會更容易。例如。
for product in response.css('.product-list__item'):
name = product.css("h2.label.title::text").extract()
# ...
因此,您可以獲得這樣的價格和美分:
>>> product.css('.product-pricing__main-price ::text')
['2', ',99€']
>>> ''.join(product.css('.product-pricing__main-price ::text').getall())
'2,99€'
添加回答
舉報
0/150
提交
取消