标签:yield 新浪 time() pre eval AC logger text style
崔庆才
学到的东西。
1 习惯用logger,而不是用print
self.logger.debug(‘{}开始解析‘.format(response.url))
2 习惯用正则表达式
s = ‘5分钟前‘ if re.match(‘\d+分钟前‘,s): minute = re.match(‘(\d+)‘,s).group(1) datetime = time.strftime(‘%Y-%m-%d %H:%M‘,time.localtime(time.time()-float(minute)*60)) print(datetime)
3 time模块都快忘干净了吧
4 eval的妙用,还可以这样用!
注释的是lowB代码。如果字段更多,更显的low。
学到了
def parse_detail(self,response): self.logger.debug(‘{}开始解析‘.format(response.url)) item = YQtem() title = response.css(‘body > div.wrap > div.mainbox > div.main2 > div.left > div.title > strong > a::text‘).extract_first() author = response.css(‘body > div.wrap > div.mainbox > div.main2 > div.right > div.autherinfo > div.au_name > p:nth-child(2) > a::text‘).extract_first() popularity = response.css(‘body > div.wrap > div.mainbox > div.main2 > div.left > div.num > table > tbody > tr > td:nth-child(2)::text‘).extract_first() count = response.css(‘body > div.wrap > div.mainbox > div.main2 > div.left > div.num > table > tbody > tr > td:nth-child(4)::text‘).extract_first() # item[‘title‘] = title # item[‘author‘] = author # item[‘popularity‘] = popularity # item[‘count‘] = count for field in item.fields: item[field] = eval(field) yield item
标签:yield 新浪 time() pre eval AC logger text style
原文地址:https://www.cnblogs.com/654321cc/p/8976944.html