Home Backend Development Python Tutorial 在Linux系统上安装Python的Scrapy框架的教程

在Linux系统上安装Python的Scrapy框架的教程

Jun 10, 2016 pm 03:10 PM
python scrapy

这是一款提取网站数据的开源工具。Scrapy框架用Python开发而成,它使抓取工作又快又简单,且可扩展。我们已经在virtual box中创建一台虚拟机(VM)并且在上面安装了Ubuntu 14.04 LTS。
安装 Scrapy
Scrapy依赖于Python、开发库和pip。Python最新的版本已经在Ubuntu上预装了。因此我们在安装Scrapy之前只需安装pip和python开发库就可以了。

pip是作为python包索引器easy_install的替代品,用于安装和管理Python包。pip包的安装可见图 1。

  sudo apt-get install python-pip
Copy after login

201561193734623.png (530×109)

图:1 pip安装

我们必须要用下面的命令安装python开发库。如果包没有安装那么就会在安装scrapy框架的时候报关于python.h头文件的错误。

  sudo apt-get install python-dev
Copy after login

201561193801448.png (513×74)

图:2 Python 开发库

scrapy框架既可从deb包安装也可以从源码安装。在图3中我们用pip(Python 包管理器)安装了deb包了。

  sudo pip install scrapy 
Copy after login

201561193823875.png (507×126)

图:3 Scrapy 安装

图4中scrapy的成功安装需要一些时间。

201561193846944.png (572×108)

图:4 成功安装Scrapy框架
使用scrapy框架提取数据
基础教程

我们将用scrapy从fatwallet.com上提取商店名称(卖卡的店)。首先,我们使用下面的命令新建一个scrapy项目“store name”, 见图5。

$sudo scrapy startproject store_name
Copy after login

201561193909068.png (553×341)

图:5 Scrapy框架新建项目

上面的命令在当前路径创建了一个“store_name”的目录。项目主目录下包含的文件/文件夹见图6。

 $sudo ls –lR store_name
Copy after login

201561193933392.png (488×336)

图:6 store_name项目的内容

每个文件/文件夹的概要如下:

  • scrapy.cfg 是项目配置文件
  • store_name/ 主目录下的另一个文件夹。 这个目录包含了项目的python代码
  • store_name/items.py 包含了将由蜘蛛爬取的项目
  • store_name/pipelines.py 是管道文件
  • store_name/settings.py 是项目的配置文件
  • store_name/spiders/, 包含了用于爬取的蜘蛛

由于我们要从fatwallet.com上如提取店名,因此我们如下修改文件(LCTT 译注:这里没说明是哪个文件,译者认为应该是 items.py)。

  import scrapy
  class StoreNameItem(scrapy.Item):
    name = scrapy.Field()  # 取出卡片商店的名称

Copy after login

之后我们要在项目的store_name/spiders/文件夹下写一个新的蜘蛛。蜘蛛是一个python类,它包含了下面几个必须的属性:

  • 蜘蛛名 (name )
  • 爬取起点url (start_urls)
  • 包含了从响应中提取需要内容相应的正则表达式的解析方法。解析方法对爬虫而言很重要。

我们在storename/spiders/目录下创建了“storename.py”爬虫,并添加如下的代码来从fatwallet.com上提取店名。爬虫的输出写到文件(StoreName.txt)中,见图7。

  from scrapy.selector import Selector
  from scrapy.spider import BaseSpider
  from scrapy.http import Request
  from scrapy.http import FormRequest
  import re
  class StoreNameItem(BaseSpider):
  name = "storename"
  allowed_domains = ["fatwallet.com"]
  start_urls = ["http://fatwallet.com/cash-back-shopping/"]
  def parse(self,response):
  output = open('StoreName.txt','w')
  resp = Selector(response)
  tags = resp.xpath('//tr[@class="storeListRow"]|\
       //tr[@class="storeListRow even"]|\
       //tr[@class="storeListRow even last"]|\
       //tr[@class="storeListRow last"]').extract()
  for i in tags:
  i = i.encode('utf-8', 'ignore').strip()
  store_name = ''
  if re.search(r"class=\"storeListStoreName\">.*&#63;<",i,re.I|re.S):
  store_name = re.search(r"class=\"storeListStoreName\">.*&#63;<",i,re.I|re.S).group()
  store_name = re.search(r">.*&#63;<",store_name,re.I|re.S).group()
  store_name = re.sub(r'>',"",re.sub(r'<',"",store_name,re.I))
  store_name = re.sub(r'&',"&",re.sub(r'&',"&",store_name,re.I))
  #print store_name
  output.write(store_name+""+"\n")
Copy after login
  

201561193954301.png (293×386)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI has started to provide developers with access to extended context windows and cost-saving features, starting with the Gemini 1.5 Pro large language model (LLM). Previously available through a waitlist, the full 2 million token context windo

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download DeepSeek Xiaomi? Search for "DeepSeek" in the Xiaomi App Store. If it is not found, continue to step 2. Identify your needs (search files, data analysis), and find the corresponding tools (such as file managers, data analysis software) that include DeepSeek functions.

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

The key to using DeepSeek effectively is to ask questions clearly: express the questions directly and specifically. Provide specific details and background information. For complex inquiries, multiple angles and refute opinions are included. Focus on specific aspects, such as performance bottlenecks in code. Keep a critical thinking about the answers you get and make judgments based on your expertise.

How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

Just use the search function that comes with DeepSeek. Its powerful semantic analysis algorithm can accurately understand the search intention and provide relevant information. However, for searches that are unpopular, latest information or problems that need to be considered, it is necessary to adjust keywords or use more specific descriptions, combine them with other real-time information sources, and understand that DeepSeek is just a tool that requires active, clear and refined search strategies.

How to program deepseek How to program deepseek Feb 19, 2025 pm 05:36 PM

DeepSeek is not a programming language, but a deep search concept. Implementing DeepSeek requires selection based on existing languages. For different application scenarios, it is necessary to choose the appropriate language and algorithms, and combine machine learning technology. Code quality, maintainability, and testing are crucial. Only by choosing the right programming language, algorithms and tools according to your needs and writing high-quality code can DeepSeek be successfully implemented.

How to use deepseek to settle accounts How to use deepseek to settle accounts Feb 19, 2025 pm 04:36 PM

Question: Is DeepSeek available for accounting? Answer: No, it is a data mining and analysis tool that can be used to analyze financial data, but it does not have the accounting record and report generation functions of accounting software. Using DeepSeek to analyze financial data requires writing code to process data with knowledge of data structures, algorithms, and DeepSeek APIs to consider potential problems (e.g. programming knowledge, learning curves, data quality)

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

See all articles