win7+Python3.5下scrapy的安装方法

  • Post category:Python

针对“win7+Python3.5下scrapy的安装方法”的完整攻略,我提供以下步骤:

安装Python3.5

首先需要安装Python3.5,可以在官网下载 https://www.python.org/downloads/release/python-35/

安装pip

pip是Python的包管理器,可以使用cmd命令行或PowerShell输入以下命令安装:

python get-pip.py

安装scrapy

可以通过pip安装该工具。打开cmd命令行或PowerShell输入以下命令进行安装:

pip install scrapy

可能还需要继续安装一些依赖包,可以通过以下命令安装:

pip install wheel
pip install twisted
pip install pypiwin32

验证scrapy

安装完成后,验证scrapy是否正确安装。可以通过以下命令创建一个新的scrapy项目:

scrapy startproject example

如果在当前路径下出现一个example目录,就说明安装成功了。

示例说明

下面提供两个scrapy的示例,一个简单的爬虫和一个爬取动态页面的实例。

示例1: 简单的爬虫

这个示例爬取github上的scrapy项目的详细信息,包括星级、贡献者、fork等信息。

假设我们要爬取的页面为: https://github.com/scrapy/scrapy

首先在example目录下的example目录中创建一个spiders目录,并在其中创建一个文件github.py。在github.py文件中输入以下代码:

import scrapy

class GithubSpider(scrapy.Spider):
    name = "github"
    start_urls = ["https://github.com/scrapy/scrapy"]

    def parse(self, response):
        yield {
            "stars": response.xpath('//a[@class="social-count js-social-count"]/text()')
            .extract_first()
            .strip()
        }

这个代码定义了一个名叫github的爬虫,并指定了爬取的起始URL以及定义了一个解析函数parse,用于解析HTML源代码中的数据。

接下来在终端中运行以下命令:

scrapy crawl github -o result.json

这个命令会启动github爬虫,并将爬取的数据保存为JSON格式的文件result.json

示例2: 爬取动态页面

这个示例爬取了北京市公共自行车共享单车的实时数据。由于数据是动态生成的,因此该示例需要使用selenium模块和PhantomJS浏览器。

先安装selenium模块和PhantomJS浏览器,可以在cmd命令行或PowerShell输入以下命令:

pip install selenium

下载PhantomJS浏览器,可以在官网下载 http://phantomjs.org/download.html

将下载后的PhantomJS浏览器的解压文件放置到Python3.5的安装目录下的Scripts目录里,例如:C:\Python35\Scripts。

接下来在example目录下的example目录中创建一个spiders目录,并在其中创建一个文件bike.py。在bike.py文件中输入以下代码:

import scrapy
from scrapy.selector import Selector
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from bike.items import BikeItem
from selenium import webdriver
from scrapy.http import TextResponse
from time import sleep

class BikeSpider(CrawlSpider):
    name = "bike"
    allowed_domains = ["beijing.publicbikesystem.net"]
    start_urls = ["http://beijing.publicbikesystem.net/"]

    rules = [
        Rule(
            SgmlLinkExtractor(allow=("/view/*",)), callback="parse_item", follow=True
        )
    ]

    def __init__(self, *args, **kwargs):
        super(BikeSpider, self).__init__(*args, **kwargs)
        self.driver = webdriver.PhantomJS()

    def __del__(self):
        self.driver.quit()

    def parse_item(self, response):
        url = response.url
        self.driver.get(url)

        sleep(10) # 等待页面加载
        body = self.driver.page_source

        item = BikeItem()
        item["station"] = Selector(text=body).xpath('//td/text()').extract()
        item["available_bikes"] = Selector(text=body).xpath('//td/span/text()').extract()
        return item

这个代码定义了一个名叫bike的爬虫,并指定了爬取的起始URL以及定义了一个解析函数parse_item,用于解析HTML源代码中的数据。

最后在终端中运行以下命令:

scrapy crawl bike -o result.json

这个命令会启动bike爬虫,并将爬取的数据保存为JSON格式的文件result.json

以上两个示例可以帮助你快速对scrapy有一个初步的了解,更加高级的内容可以参考官方文档。