爬取新浪微博

https://github.com/factsbenchmarks/Weibo

学到的东西。

  1 习惯用logger,而不是用print

self.logger.debug('{}开始解析'.format(response.url))

  2 习惯用正则表达式

  这是在pipeline清理数据时用到的

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

   5 formrequest 的用法

data = {
                    'mp': str(self.max_page),
                    'page': str(page)
                }
yield FormRequest(url, callback=self.parse_index, formdata=data)
原文地址:https://www.cnblogs.com/654321cc/p/8976944.html