Python3 Environment Installation Scrapy Crawler Framework Process

Source: Internet
Author: User

Python3 Environment Installation Scrapy Crawler Framework Process

1. Installing Wheel

Pip Install Wheel

Installation check:

2. Install lxml pip Install LXML-4.2.1-CP36-CP36M-WIN_AMD64.WHL

Go to https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml download the corresponding Python version

Select the version to download: Cp36 is the Python version, here my version is python36, the operating system is Win64 bit, so i download the file

Lxml-4.2.1-cp36-cp36m-win_amd64.whl

Download the Python36 lxml version

Switch to the downloaded directory under DOS (CD D:\soft\Scrapy environment Setup)

Execute: Pip install LXML-4.2.1-CP36-CP36M-WIN_AMD64.WHL

3. Installing Zope.interface

To the official website https://pypi.python.org/pypi/zope.interface#downloads download the corresponding version of the wheel file, if not logged in, you need to log in.

Under DOS, switch to the downloaded directory (CD D:\soft\Scrapy environment setup).

Execute: Pip install ZOPE.INTERFACE-4.5.0-CP36-CP36M-WIN_AMD64.WHL

4. Installing Pyopenssl

Official website Download wheel file, https://pypi.python.org/pypi/pyOpenSSL#downloads

Download PYOPENSSL-17.5.0-PY2.PY3-NONE-ANY.WHL

Under DOS, switch to the downloaded directory (CD D:\soft\Scrapy environment setup).

Execute: Pip install PYOPENSSL-17.5.0-PY2.PY3-NONE-ANY.WHL

5. Installing twisted

Similarly, http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted下载wheel文件, with PIP installation.
such as Python version 3.6, Windows 64-bit system, download
TWISTED-17.9.0-CP36-CP36M-WIN_AMD64.WHL, and then Pip installs.

Switch to the downloaded directory under DOS

Execute: Pip install TWISTED-17.9.0-CP36-CP36M-WIN_AMD64.WHL

6. Installing Pywin32

Download the corresponding version of the installation package from the official website https://sourceforge.net/projects/pywin32/files/pywin32/Build%20220/.

Download

Installation:

When you install, you will receive a prompt python3.6-32 does not exist in the registry.

Methods of dealing with problems of python3.6 installation Pywin32 under WINDOS64 bit

Method One

The mistake of this newspaper is because of the registry problem, so Baidu to the following section of code

After this code execution can automatically add the py3.6 installation directory directly into the registry, check the following registry, does appear.

The first step is to press "Win+r" or click on the Start menu, locate the run, and enter "regedit" in the Run input box, as shown in:

hkey-current_user–software–python–pythoncore– will find the 3.6-32 folder

Method Two: put the registration form 3.6 Export, save name as 3.6-32 , then import the registry and restart your computer.

Once you're done, you can install pywin32-220.win-amd64-py3.6

7. Install scrapy pip Install Scrapy

Finally install Scrapy, still use PIP, command as follows:

Scrapy Installation Success Verification

Making scrapy crawler altogether need 4 Step:

    • New Project (Scrapy startproject XXX): Create a new crawler project
    • Clear goals (write items.py): Identify the target you want to crawl
    • Making crawlers (spiders/xxspider.py): Making crawlers start crawling Web pages
    • Storage content (pipelines.py): Design Pipeline Store crawl content

Steps:

I. New Project (Scrapy Startproject)

1. Create a new folder to hold the Scrapy project (d:\ crawler _script\scrapy_project).

2. Go to this directory from DOS

3. Create a new crawler project: Scrapy startproject Myspiter

Here's a brief look at the role of each major file:

SCRAPY.CFG: Configuration file for Project

myspider/: The project's Python module, which will reference the code from here

myspider/items.py: Target file for Project

myspider/pipelines.py: Pipeline file for Project

myspider/settings.py: Setup file for Project

myspider/spiders/: Storage Crawler Code Directory

Ii. Clear Objectives (myspider/items.py)

We intend to crawl: the names, titles and personal information of all lecturers in the http://www.itcast.cn/channel/teacher.shtml website.

Open the items.py in the Myspider directory

Item defines a structured data field that is used to hold the crawled data, somewhat like the dict in Python, but provides some additional protection from errors.

You can create a scrapy by creating a. The Item class, and the definition type is scrapy. Field's class attribute to define an item (which can be understood as a mapping relationship similar to ORM).

Next, create a Itcastitem class, and build the item model.

Import Scrapy

Class Itcastitem (Scrapy. Item):

Name = Scrapy. Field ()

Level = Scrapy. Field ()

info = scrapy. Field ()

Third, Production crawler (spiders/itcastspider.py)

The crawler function takes two steps:

1. Crawling data
    • Enter the command under the current directory, mySpider/spider create a crawler named under the directory itcast , and specify the scope of the crawl domain:
    • Open the itcast.py in the Myspider/spider directory and add the following code by default:
scrapy genspider itcast "itcast.cn"
Import scrapy
 
Class Itcastspider (Scrapy. Spider):
"Itcast"
    allowed_domains = ["Itcast.cn"]
    start_urls = (
' http://www.itcast.cn/',
    )
 
Def parse (self, Response):
Pass
In fact, we can also create itcast.py and write the above code, but the use of commands can eliminate the hassle of writing fixed code

To build a spider, you have to use Scrapy. The spider class creates a subclass and determines three mandatory properties and a method.

    • name = "": The name of the crawler must be unique and different names must be defined in different crawlers.
    • allow_domains = []is the domain name scope of the search, that is, the crawler's constrained area, which specifies that the crawler crawl only the page under this domain name, the nonexistent URL will be ignored.
    • start_urls = (): Crawled URL tuple/list. Crawlers start crawling data from here, so the first downloaded data will start with these URLs. Other sub-URLs will be generated from these starting URLs for inheritance.
    • parse(self, response): Parsing method, each initial URL completes the download will be called, when the call passed from each URL returned to the response object as the only parameter, the main role is as follows:
    1. Responsible for parsing the returned web page data (response.body), extracting structured data (Generate item)
    2. Generate a URL request that requires the next page.
Modify the value of Start_urls to the first URL that needs to be crawled
start_urls = ("Http://www.itcast.cn/channel/teacher.shtml",)
Modify the Parse () method
Def parse (self, Response):
"Teacher.html"
' W ').write(response.body)

Then run a look and execute it in the Myspider directory:

scrapy crawl itcast

Yes, it is itcast, look at the code above, it is the Name property of the Itcastspider class, which is the scrapy genspider unique crawler name that uses the command.

After running, if the printed log appears [scrapy] INFO: Spider closed (finished) , the execution is completed. After that, a teacher.html file appears in the current folder, which is the full source code information for the page we just crawled.

2. fetching Data

    • Crawl the entire page, the next step is to take the process, the first observation of the page source code:

<div class= "Li_txt" >
 
 
<p>  </p>
Save the data scrapy the simplest way to save information is mainly four,-o output the specified format of the file, the command is as follows:
# JSON format, default Unicode encoding
scrapy crawl itcast -o teachers.json 
# JSON lines format, default Unicode encoding
scrapy crawl itcast -o teachers.jsonl 
# CSV comma-expression, available in Excel Open
scrapy crawl itcast -o teachers.csv 
# XML format
scrapy crawl itcast -o teachers.xml


Python3 Environment Installation Scrapy Crawler Framework Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.