Scrapy 入门

Scrapy

https://docs.scrapy.org/en/latest/intro/overview.htmlcss


Scrapy is an application framework for crawling web sites and extracting structured data which can be used for a wide range of useful applications, like data mining, information processing or historical archival.html

Even though Scrapy was originally designed for web scraping, it can also be used to extract data using APIs (such as Amazon Associates Web Services) or as a general purpose web crawler.python



安装

一、 安装python 2.7web

二、安装 pipjson

https://jingyan.baidu.com/article/e73e26c0d94e0524adb6a7ff.htmlapi

进入命令行,而后把目录切换到python的安装目录下的Script文件夹下,运行 easy_inatall pipapp

三、 安装scrpayscrapy

https://docs.scrapy.org/en/latest/intro/install.htmlide

pip install Scrapy
若是安装不成功,多尝试两次

运行报错:url

ImportError: No module named win32api

解决办法:

https://blog.csdn.net/jianhong1990/article/details/46406499

下载安装: http://sourceforge.net/projects/pywin32/files%2Fpywin32/


运行

保存以下脚本为

quotes_spider.py
import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://quotes.toscrape.com/tag/humor/',
    ]

    def parse(self, response):
        for quote in response.css('div.quote'):
            yield {
                'text': quote.css('span.text::text').extract_first(),
                'author': quote.xpath('span/small/text()').extract_first(),
            }

        next_page = response.css('li.next a::attr("href")').extract_first()
        if next_page is not None:
            yield response.follow(next_page, self.parse)


在此目录下,运行

scrapy runspider quotes_spider.py -o quotes.json



相关知识

CSS选择器

http://www.w3school.com.cn/cssref/css_selectors.asp

image


image


image



XPATH

http://www.cnblogs.com/fdszlzl/archive/2009/06/02/1494836.html



Python yield

https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/


image


image

相关文章
相关标签/搜索