0
我正在尝试使用Scrapy/Python编写一个爬网程序,它从页面读取一些值。Scrapy/Python:以收益率处理值
然后我希望这个履带式存储器在最高和最低值分开的字段中存储。到目前为止,我能够从页面读取值(请参阅下面的代码),但我不确定如何计算最低和最高值并存储在单独的字段中?
举例来说,假设履带读取页面,并返回这些值
- burvale分数= 75.25
- 里士满分数= 85.04
- 索马诺分数= ''(值缺失)
- 图森分数= 90.67
- 云的得分= 50.00
所以,我想填充....
- 'highestscore':90.67
- 'lowestscore':50.00
我该怎么办呢?我需要使用数组吗?把所有的值放在数组中,然后选择最高/最低?
另外,请注意,有2 yield
在我的代码....底部yield
正在提供的网址抓取,并且第一yield
实际抓取/收集从由底部yield
提供的每个网址的值
任何帮助,非常感谢。如果可以,请提供代码示例。
这是我的代码到目前为止 ....我存储-1,在缺少值的情况下。
class MySpider(BaseSpider):
name = "courses"
start_urls = ['http://www.example.com/all-courses-listing']
allowed_domains = ["example.com"]
def parse(self, response):
hxs = Selector(response)
#for courses in response.xpath(response.body):
for courses in response.xpath("//meta"):
yield {
'pagetype': courses.xpath('//meta[@name="pagetype"]/@content').extract_first(),
'pagefeatured': courses.xpath('//meta[@name="pagefeatured"]/@content').extract_first(),
'pagedate': courses.xpath('//meta[@name="pagedate"]/@content').extract_first(),
'pagebanner': courses.xpath('//meta[@name="pagebanner"]/@content').extract_first(),
'pagetitle': courses.xpath('//meta[@name="pagetitle"]/@content').extract_first(),
'pageurl': courses.xpath('//meta[@name="pageurl"]/@content').extract_first(),
'pagedescription': courses.xpath('//meta[@name="pagedescription"]/@content').extract_first(),
'pageid': courses.xpath('//meta[@name="pageid"]/@content').extract_first(),
'courseatarburvale': float(courses.xpath('//meta[@name="courseatar-burvale"]/@content').extract_first('').strip() or -1),
'courseatarrichmond': float(courses.xpath('//meta[@name="courseatar-richmond"]/@content').extract_first('').strip() or -1),
'courseatarsomano': float(courses.xpath('//meta[@name="courseatar-somano"]/@content').extract_first('').strip() or -1),
'courseatartucson': float(courses.xpath('//meta[@name="courseatar-tucson"]/@content').extract_first('').strip() or -1),
'courseatarcloud': float(courses.xpath('//meta[@name="courseatar-cloud"]/@content').extract_first('').strip() or -1),
'highestscore'; ??????
'lowestscore'; ??????
}
for url in hxs.xpath('//ul[@class="scrapy"]/li/a/@href').extract():
yield Request(response.urljoin(url), callback=self.parse)
感谢@托马斯·林哈特我在一个类似的方法工作....我会回报不久...干杯 – Slyper
嗨@TomášLinhart当我尝试了你的建议,我得到了这个错误.... _IndexError:列表索引超出范围_任何想法?它抱怨这行'highestatar':values [-1],' – Slyper
@Slyper可能'values'列表是空的,如果没有从页面提取分数(即'scores'只包含-1),则可能发生这种情况。因此,请将最高分数分配代码更改为“最高分数”:值[-1]如果其他值为无并且同样适用于最低分数。 –