Python crawler Frame Scrapy Learning Note 4-------Second scrapy project

Source: Internet
Author: User
Tags xpath

1. Task one, crawl the contents of the following two URLs, write the file

http://www.dmoz.org/Computers/Programming/Languages/Python/Books/

http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/


Project

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/58/31/wKiom1SrlyvCB0O1AAS_JTtbcKA938.jpg "title=" P2-s1.png "alt=" Wkiom1srlyvcb0o1aas_jttbcka938.jpg "/>


Unlike the previous project, the rules attribute is not defined in the spider, but the parse method is defined. This method tells Scrapy what to do when grabbing the contents of start URLs. The first task we only write the content to the file.


2. Task two: Practice using XPath in the Scrapy shell

In the top-level directory of the project, enter:

Scrapy Shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/58/32/wKiom1Srm9Gw9wt2AARF9P7PZ6s473.jpg "title=" Shell.png "alt=" Wkiom1srm9gw9wt2aarf9p7pz6s473.jpg "/>


There is a loval variable called response, which stores the content that the shell crawled when it was loaded.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/58/2E/wKioL1SrnY3xQ96dAAebneLFVus941.jpg "title=" Response.png "alt=" Wkiol1srny3xq96daaebnelfvus941.jpg "/>


Practice a simple XPath


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/58/32/wKiom1Srn8nTO1s2AAKMdUwQem8295.jpg "title=" Xpath.png "alt=" Wkiom1srn8nto1s2aakmduwqem8295.jpg "/>


The XPath method returns a series of selector, and you can continue to tune the selector XPath method to dig deeper.

Finally, using CTRL + Z or typing two times quit to launch the shell


3. Task three, select title from response, link and desc and output to the console.

To overwrite our spider for this purpose

Import scrapyclass dmozspider (scrapy. Spider):    name =  "DMOZ"     allowed_domains = [" Dmoz.org "]    start_urls = [" http://www.dmoz.org/Computers/Programming/Languages /python/books/",                    "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"]     def parse (self, response):         for sel in  response.xpath ('//ul/li '):             title  = sel.xpath (' A/text () '). Extract ()              link = sel.xpath (' a @href '). Extract ()              desc = sel.xpath (' text () '). Extract ()    &NBsp;        print title, link, desc 


4. Task four: Write the title, link, and desc in JSON form to the file

Overwrite the items.py of the project top level directory

#-*-Coding:utf-8-*-# Define Here the models for your scraped items## see documentation in:# HTTP://DOC.SCRAPY.ORG/EN/L Atest/topics/items.htmlimport scrapyclass Dmozitem (scrapy. Item): title = Scrapy. Field () link = scrapy. Field () desc = scrapy. Field ()


Rewrite spider

__author__ =  ' DB ' import scrapyfrom project002.items import dmozitemclass  Dmozspider (scrapy. Spider):    name =  "DMOZ"     allowed_domains = [" Dmoz.org "]    start_urls = [" http://www.dmoz.org/Computers/Programming/Languages /python/books/",                    "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"]     def parse (self, response):         for sel in  response.xpath ('//ul/li '):             item  = dmozitem ()             item[' title ']  = sel.xpath (' A/text () '). Extract ()             &nbspitem[' link '] = sel.xpath (' a @href '). Extract ()              item[' desc '] = sel.xpath (' text () '). Extract ()              yield item


Run the spider again:

Scrapy Crawl Dmoz-o Items.json



Python crawler Frame Scrapy Learning Note 4-------Second scrapy project

Related Article

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.