我制作了这个小机器人,它通过搜索参数列表进行处理。它工作正常,直到页面上出现多个结果: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}
任何的想法?:)
函数式编程
波斯汪
相关分类