scrapy框架入门与使用

本文档详细介绍了如何安装Scrapy,并通过一个入门案例展示其基本使用。首先,通过pip安装Scrapy及相关依赖。接着,创建Scrapy项目,设置不遵循robots.txt、下载延迟和请求头。然后,定义Item结构以提取电影名和描述。在爬虫程序中,利用XPath提取数据,并使用Request进行页面迭代。最后,通过自定义管道处理并输出数据。运行项目,使用`scrapy crawl txms`启动爬虫。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.scrapy的安装

python -m pip install --upgrade pip

pip install wheel

pip install lxml

pip install twisted

pip install pywin32

pip install scrapy

2.scrapy的入门案例

1.创建项目

打开cmd,输入(默认是在C:\Users\Administrator>这个目录下,你可以自行切换):

scrapy startproject myfirstPj

cd my firstPj

scrapy genspider baidu www.baidu.com

2.修改setting

修改三项内容,第一个是不遵循机器人协议,第二个是下载间隙,由于下面的程序要下载多个页面,所以需要给一个间隙(不给也可以,只是很容易被侦测到),第三个是请求头,添加一个User-Agent,第四个是打开一个管道

ROBOTSTXT_OBEY=False
DOWNLOAD_DELAY=1DEFAULT_REQUEST_HEADERS={'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language':'en','User-Agent':'Mozilla/5.0(WindowsNT6.2;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/27.0.1453.94Safari/537.36'}ITEM_PIPELINES={'TXmovies.pipelines.TxmoviesPipeline':300,}

3.确认要提取的数据,item项

item定义你要提取的内容(定义数据结构),比如我提取的内容为电影名和电影描述,我就创建两个变量。Field方法实际上的做法是创建一个字典,给字典添加一个建,暂时不赋值,等待提取数据后再赋值。下面item的结构可以表示为:{'name':'','descripition':''}。

#-*-coding:utf-8-*-

#Defineherethemodelsforyourscrapeditems

#

#Seedocumentationin:

#https://docs.scrapy.org/en/latest/topics/items.html

importscrapy

classTxmoviesItem(scrapy.Item):

        #definethefieldsforyouritemherelike:

        #name=scrapy.Field()

        name=scrapy.Field()

        description=scrapy.Field()

4.写爬虫程序

items['name']=i.xpath('./a/@title')[0]
items['name']=i.xpath('./a/@title').extract()
items['name']=i.xpath('./a/@title').extract_first()
items['name']=i.xpath('./a/@title').get()
 
 
# -*- coding: utf-8 -*-
 
import scrapy
from ..items import TxmoviesItem
 
class TxmsSpider(scrapy.Spider):
    name = 'txms'
    allowed_domains = ['v.qq.com']
    start_urls = ['https://v.qq.com/x/bu/pagesheet/listappend=1&channel=cartoon&iarea=1&listpage=2&offse=0&pagesize=30']
    offset=0
    def parse(self, response):
        items=TxmoviesItem()
        lists=response.xpath('//div[@class="list_item"]')
        for i in lists:
            items['name']=i.xpath('./a/@title').get()
            items['description']=i.xpath('./div/div/@title').get()
            yield items
        if self.offset < 120:
            self.offset += 30
            url = 'https://v.qq.com/x/bu/pagesheet/listappend=1&channel=cartoon&iarea=1&listpage=2&offset={}&pagesize=30'.format(str(self.offset))
            yield scrapy.Request(url=url,callback=self.parse)

5.交给管道输出

# -*- coding: utf-8 -*-
# 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
 
class TxmoviesPipeline(object):
    def process_item(self, item, spider):
        print(item)
        return item

6.run,执行项目

fromscrapyimportcmdline
cmdline.execute('scrapycrawltxms'.split())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值