ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用 scrapy 爬取 微博热搜

2020-03-03 16:57:26  阅读:656  来源: 互联网

标签:xpath weibo items 爬取 item scrapy com 微博热


安装

pip install Scrapy

创建项目

scrapy startproject weiboHotSearch

UTOOLS1583039785087.png

创建爬虫

cd weiboHotSearch
scrapy genspider weibo s.weibo.com

UTOOLS1583039841905.png

UTOOLS1583039871927.png

编写Item

修改weiboHotSearch中的items.py,添加item

import scrapy


class WeibohotsearchItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass
    keyword = scrapy.Field()
    url = scrapy.Field()
    count = scrapy.Field()

编写爬虫

  1. 修改start_urls,注意为list格式

  2. 使用xpath解析数据

    xpath语法可参考https://www.w3school.com.cn/xpath/xpath_syntax.asp

    解析数据时,可运行scrapy shell "https://s.weibo.com/top/summary"调试xpath

    UTOOLS1583042532863.png

  3. 引入Item,将数据以Itme对象返回

  4. 执行scrapy crawl weibo运行爬虫

    运行结果如下:

    UTOOLS1583042613274.png

    weibo.py的完整代码

import scrapy

from weiboHotSearch.items import WeibohotsearchItem
class WeiboSpider(scrapy.Spider):
    name = 'weibo'
    allowed_domains = ['s.weibo.com']
    start_urls = ['https://s.weibo.com/top/summary']

    def parse(self, response):
        pass
        for i in response.xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr/td[2]'):
            keyword = i.xpath('a/text()').extract_first()
            url = 'https://s.weibo.com'+i.xpath('a/@href').extract_first()
            count = i.xpath('span/text()').extract_first()
            print(keyword)
            print(count)
            # print(url)
            item = WeibohotsearchItem()
            item['keyword'] = keyword
            item['url'] = url
            item['count'] = count
            yield item

保存数据

  • 使用Feed export 保存

使用以下命令即可将数据保存到items.json

scrapy crawl weibo -o items.json
cat items.json
  • 使用Item Pipeline保存

    1. 编写pipeline

      修改pipelines.py,添加保存

      class WeibohotsearchPipeline(object):
          def __init__(self):
              self.f = open('items.csv','w')
      
          def process_item(self, item, spider):
              res = item['keyword']+','+item['count']+','+item['url']+"\n"
      
              self.f.write(res)
              return item
    2. 启用item pipline

      将以下内容添加到settings.py中即可启用Pipline

      ITEM_PIPELINES = {
         'weiboHotSearch.pipelines.WeibohotsearchPipeline': 300,
      }
    3. 运行

      scrapy crawl weibo 
      cat items.csv

数据如下:

UTOOLS1583043788984.png

UTOOLS1583043848215.png

项目地址

https://gitee.com/yu-se/scrapy-test

参考文档

https://scrapy-chs.readthedocs.io/zh_CN/latest/

标签:xpath,weibo,items,爬取,item,scrapy,com,微博热
来源: https://www.cnblogs.com/lzyuid/p/12403151.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有