所以我使用python / scrapy从网页上抓取数据。基本上,网页由15个块组成,包含各种信息。我的蜘蛛重复通过每一个块来刮掉一些特定的内容。我对结果的内容感到满意,但对数据的呈现方式却不满意。我希望属于一个块的所有抓取信息都显示在一行中。您将从下面的屏幕截图中看到,同一块的结果并不是并排显示的,这就是我想要的。
def parse(self, response):
for i in response.css('span.dir'):
yield {'address': i.css('b::text').extract()}
for l in response.css('div.datos'):
yield {'area': l.css('i::text').extract()}
for x in response.css('div.opciones'):
yield {'price stable': x.css('span.eur::text').extract()}
for o in response.css('div.opciones'):
yield {'price drop': o.css('div.mp_pvpant.baja::text').extract()}
for y in response.css('div.opciones'):
yield {'price decreased': y.css('span.eur_m::text').extract()}
for u in response.css('div.datos'):
yield {'link': u.css('a::attr(href)').extract_first()}
答案 0 :(得分:1)
如果每行的结果数相同,则可以执行此操作:
def parse(self, response):
addresses = []
areas = []
prices_stable = []
prices_drop = []
prices_decreased = []
links = []
for i in response.css('span.dir'):
addresses.append(i.css('b::text').extract())
for l in response.css('div.datos'):
areas.append(l.css('i::text').extract())
for x in response.css('div.opciones'):
prices_stable.append(x.css('span.eur::text').extract())
for o in response.css('div.opciones'):
prices_drop.append(o.css('div.mp_pvpant.baja::text').extract())
for y in response.css('div.opciones'):
prices_decreased.append(y.css('span.eur_m::text').extract())
for u in response.css('div.datos'):
links.append(u.css('a::attr(href)').extract_first())
for address, area, price_stable, price_drop, price_decreased, link in zip(addresses, areas, prices_stable, prices_drop, prices_decreased, links):
yield {
'address': address,
'area': area,
'price_stable': price_stable,
'price_drop': price_drop,
'price_decreased': price_decreased,
'link': link,
}
答案 1 :(得分:0)
您可以将提取的值附加到列表中,然后生成相同的内容,例如
def parse(self, response):
# create more lists for the remaining fields
address = []
area = []
for i in response.css('span.dir'):
address.append(i.css('b::text').extract())
yield {'address':address}
for l in response.css('div.datos'):
area.append(l.css('i::text').extract())
yield {'area':area}