Scrapy 基本用法

本文介绍了如何使用Scrapy框架创建一个Python项目,从百度网站抓取实时新闻,并将数据存储到CSV和Excel文件中,包括设置虚拟环境、安装依赖、编写爬虫和配置itempipeline。
摘要由CSDN通过智能技术生成

1. 创建项目:scrapy startproject scrapy_test

2. 进入项目目录:cd scrapy_test

3. 生成spiders文件:scrapy genspider baidu baidu.com  (scrapy genspider + 名称 + 域名)

4. 新建虚拟环境 : settings -> project -> python interpreter -> Add interpreter ->(默认在本目录下创建一个venv)。打开终端,出现 (venv), 说明虚拟环境已经激活 例如:(venv) PS D:\test_code\scr\test_t1>

5. 在虚拟环境下安装 scrapy    pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

6. 测试代码:

baidu.py:

import scrapy
from scrapy import Selector
from scrapy_test.items import ScrapyTestItem

class BaiduSpider(scrapy.Spider):
    name = "baidu"
    allowed_domains = ["baidu.com"]
    start_urls = ["https://top.baidu.com/board?tab=realtime"]

    def parse(self, response):
        sel = Selector(response)
        l1 = sel.css('#sanRoot > main > div.container.right-container_2EFJr > div > div > div > div.content_1YWBm > a > div.c-single-text-ellipsis::text').extract()

        for i in l1:
            news = ScrapyTestItem()
            news['news'] = i

            yield news

items.py:

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class ScrapyTestItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    news = scrapy.Field()

settings.py 添加 USER_AGENT

USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"



# 放开注释
ITEM_PIPELINES = {
   "scrapy_test.pipelines.ScrapyTestPipeline": 300,
}

pipelines.py

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import openpyxl

class ScrapyTestPipeline:

    def __init__(self):
        self.wb = openpyxl.Workbook()
        self.ws = self.wb.active
        self.ws.title = 'news_hot'
        self.ws.append(('news_titile', 'test'))

    def close_spider(self, spider):
        self.wb.save('news_hot.xlsx')

    def process_item(self, item, spider):
        news = item.get('news', '')
        self.ws.append((news, ''))
        return item

7. 生成 .csv 文件: scrapy crawl baidu -o test.csv

    生成 excel 文件: scrapy crawl baidu

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值