zoukankan      html  css  js  c++  java
  • 爬取新浪微博

    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)
  • 相关阅读:
    JavaScript高级程序设计:第十二章
    JavaScript高级程序设计:第九章
    PageHelper的使用方法
    dbutils的环境搭建以及使用
    Spring的xml中引入其他文件
    Spring AOP
    SpringMVC拦截器
    中文乱码的过滤器
    SpringMVC的处理器全局异常处理类
    SpringMVC返回一个JSON对象到界面
  • 原文地址:https://www.cnblogs.com/654321cc/p/8976944.html
Copyright © 2011-2022 走看看