我正在写一个网络爬虫,我的Python已经锈得要命了,所以我只是想知道是否有一个较短的语法来完成以下任务。。。你知道吗
def parse(self, response):
prc_path = '//span[@class="result-meta"]/span[@class="result-price"]/text()'
sqf_path = '//span[@class="result-meta"]/span[@class="housing"]/text()'
loc_path = '//span[@class="result-meta"]/span[@class="result-hood"]/text()'
prc_resp = response.xpath(prc_path).extract_first()
sqf_resp = response.xpath(sqf_path).extract_first()
loc_resp = response.xpath(loc_path).extract_first()
if sqf_resp and loc_resp:
yield {
'prc': response.xpath(prc_path).extract_first(),
'sqf': response.xpath(sqf_path).extract_first(),
'loc': response.xpath(loc_path).extract_first()
}
elif sqf_resp:
yield {
'prc': response.xpath(prc_path).extract_first(),
'sqf': response.xpath(sqf_path).extract_first()
}
else:
yield {
'prc': response.xpath(prc_path).extract_first(),
'loc': response.xpath(loc_path).extract_first()
}
正如你所看到的,重复的次数很多,我想保持尽可能干燥。你知道吗
您可以创建字典,然后向其中添加适当的条目。你知道吗
你也可以通过听写理解来计算出
extract_path
位。你知道吗在早期版本的Python中,这将是:
我会用一个查找地图:
相关问题 更多 >
编程相关推荐