亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Scrapy/忽略空項目

Scrapy/忽略空項目

瀟瀟雨雨 2021-08-01 04:27:11
我制作了這個小機器人,它通過搜索參數列表進行處理。它工作正常,直到頁面上出現多個結果: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€'


查看完整回答
反對 回復 2021-08-03
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

最后,您可以通過您不想要的事件過濾您的列表:

list(filter(lambda a: a != '', yourList))


查看完整回答
反對 回復 2021-08-03
  • 2 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號